Mongosh --eval freezes the shell

Hi,
I frequently used to run ‘mongo’ with --eval parameter, mostly in some automation scripts, for example simple ‘mongo --eval 1’ to see if mongod is already fully started.
When I try the same with mongosh the execution of command never ends. It prints some welcome information, then the literal ‘1’ and freezes. I can’t stop it by either ctrl+c, ctrl+d or ctrl+z. Only killing it from another shell helps.
IT is the same with any other command.
Is this something that you know about? Maybe I’m doing something wrong?

Thanks

1 Like

@Andrzej_Podgorski How are you killing the process (i.e. with which signal)? Can you see if there’s a spike in CPU usage? Which version of mongosh are you using? Can you look at the log file (~/.mongodb/mongosh/_log) and see if there’s any relevant information in there?

1 Like

I’m able to kill it even with SIGTERM. I don’t see any spike in CPU usage.
I see a problem on both mongosh 1.0.4 and 1.0.5 on Ubuntu 18.04 on a few different machines. Usage of --nodb or --norc does not change the behavior.
I don’t see anything relevant in the log. The last entry is
{“level”:30,“time”:1630050655432,“pid”:15175,“hostname”:"",“name”:“mongosh”,“msg”:“mongosh:evaluate-input {“input”:“1”}”,“v”:1}

And I have the same on Windows using powershell

any ideas here folks?

@Andrzej_Podgorski The problem is that this is hard to debug without a reproduction – I’m also running Ubuntu but haven’t had an issue like this so far.

I guess you could explicitly use something like mongosh --eval "print(1); exit();" or mongosh --eval "print(1); process.exit();" to explicitly stop the shell? I know it’s not a great solution, but I’m also not sure how else to move forward here.

If you are willing to debug this in depth (which we are grateful for but obviously don’t expect of you) on Ubuntu, running mongosh under strace might be pretty helpful (although the log it generates would presumably be huge).

2 Likes

Hello,
I have a similar issue. After executing command(s) passed through --eval, mongosh freezes. But in my case it only does it occasionally and it doesn’t freeze forever. Sometimes it freezes for around 30 seconds, sometimes for several minutes. Often it freezes several times in succession and then works fine for a long time (hours), until it breaks again (although I look at it for several days now, I didn’t find anything it may be related to, yet).

When I run mongosh --quiet --eval 'print(1);' and it freezes, it can’t be stopped by Ctrl-C, but kill <PID> from another shell works, exaxctly as Andrzej_Podgorski described.

However, when I run mongosh --quiet --eval 'print(1); exit();', it CAN be stopped by Ctrl-C, in which case it prints this message and exits:

$ mongosh --quiet --eval 'print(1); exit();'
1
^CError: Asynchronous execution was interrupted by `SIGINT`

I tried to run it with strace -tt several times. Outputs can be found on the links below (as a new user, I cannot upload it here). The first one is of a run when it freezes for a very long time (I killed it after 30 minutes), the second one, for comparison, of a run when it freezes for just 15-20 seconds.

I know this will be very difficult to debug. Hopefully the error message and strace output will help.

I’m running it on CentOS 7 (two instances with similar configuration, it happens on both).
mognod v. 5.0.3
mongosh v. 1.0.7
Legacy mongo shell on the same system works well.

1 Like

Sorry Anna, I somewhat forgot to respond there, but I think that @Vaclav_Bartos described all of my issues and provided needed traces. Thanks @Vaclav_Bartos for that!

Hi all,

This issue has gone stale with no real progress. However, the problem seems to be getting worse. With the adoption of mongosh under bitnami charts, more and more people are complaining about it (see [bitnami/mongodb] mongosh connections (used in several probes) freeze and consuming a lot of cpu and memory ressources · Issue #10264 · bitnami/charts · GitHub). How can we get some traction here? @Anna_Henningsen

Thx,
Andrei

I have the same issue, with mongodb v5.0.2 and mongosh v1.0.5, running with the official docker image(Docker).
In my case, if I put exit() at the end of my script, it will also freezes but can be terminated with Ctrl-C, just as @Vaclav_Bartos said; And if I put process.exit(0) at the end of my script, it never freezes, which is my currently workaround.
I guess this problem is related to that the JS event loop is not empty after excuting all codes in the script, so the JS process does not terminate. I made that guess because terminating the JS process with process.exit(0) can solve this problem.

1 Like

I just installed mongosh and am looking at refactoring some scripts to use mongosh instead of mongo for automation, and I ran into this problem very hard. I’m responding to this comment specifically because not only am I getting mongosh hanging on evals as other folks are, but I’m getting it even when the eval includes a terminal exit(); call. When I use mongosh interactively instead of calling it in a shell script, I still get it to hang when I issue an exit or quit command. I’m using mongosh 1.6.1 on a MongoDB 6.0.1 server running on an AWS CentOS 7 instance. The only way I can exit mongosh reliably is with ctrl-C, which isn’t something I can use for scripting and automation.

This is a major problem for me; the new shell program is (still) unusable but the old one isn’t supported.

I’d be willing to work with you (or anyone else at MongoDB) on debugging in depth given the show-stopping nature of this bug. You can contact me with messaging on the forum but I’m happy to share emails or anything else for a tighter communication loop.

@Tyler_Elkink The ideal scenario is probably still a way of fully reproducing this, but I also just tried this on an AWS CentOS 7 machine and couldn’t get it to fail, so yeah, debugging this more directly sounds good. You can contact me at anna.henningsen@mongodb.com and we can try to either debug this over email or set up a short call to look at the problem in a “live” environment.

For those who find this thread in the future; CentOS 7 seems to be the common named element across the “mongosh hangs” problem; since Oracle’s dropping support for 7 shortly and has destabilized future releases this problem should age out. I’m working with a MongoDB employee on tracking down the actual bug, but for those of us dealing with the problem in the meantime, and in my instance at least;

TL;DR using process.exit() instead of exit will successfully terminate mongosh.

Updating with further fixes;
thanks to Anna’s help, any of the following fixes mongosh failing to terminate on exit (hanging). Tested this on CentOS 7, using MongoDB version 5.0.9, and mongosh 1.5.0, though I suspect any MongoDB 5 and mongosh <1.6 will have the same issue.

  1. prepend to any eval commands, or issue open entering mongosh, the following command; db._mongo._instanceState.evaluationListener.ioProvider.close = () => {};
  2. terminate any eval or mongosh session with process.exit() instead of exit
  3. upgrade to mongosh version 1.6.1

If you can manage it, I’d suggest the upgrade as the best option.

1 Like