Mongosh --eval freezes the shell

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?


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).


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();'
^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 How can we get some traction here? @Anna_Henningsen


I have the same issue, with mongodb v5.0.2 and mongosh v1.0.5, running with the official docker image(Docker Hub).
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