Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-46028

non-interactive mongo shell tries to write history file before aborting

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Trivial - P5 Trivial - P5
    • None
    • Affects Version/s: None
    • Component/s: Shell
    • Labels:
      None
    • Server Tooling & Methods
    • ALL
    • Hide
      1. Spin up a mongod
      2. Connect to it using mongo shell 3.6 or above:
        mongo --eval 'while(true) {printjson(new Date()); sleep(2000)}'
        
      3. Press Ctrl-c to interrupt
      4. You'll see an error message like the following:
        2020-02-07T16:00:22.482+1100 E -        [main] Error saving history file: FileOpenFailed: Unable to open() file : No such file or directory
        2020-02-07T16:00:22.482+1100 I CONTROL  [main] shutting down with code:0
        
      Show
      Spin up a mongod Connect to it using mongo shell 3.6 or above: mongo --eval ' while ( true ) {printjson( new Date()); sleep(2000)}' Press Ctrl-c to interrupt You'll see an error message like the following: 2020-02-07T16:00:22.482+1100 E - [main] Error saving history file: FileOpenFailed: Unable to open() file : No such file or directory 2020-02-07T16:00:22.482+1100 I CONTROL [main] shutting down with code:0

      Hi,

      If I understand the following source code snippet correctly, the mongo shell will not write the history file if the process is non-interactive but ::shellHistoryDone() is registered as a shutdown task at the very beginning of _main.

      //  mongo/src/mongo/shell/dbshell.cpp
      int _main(int argc, char* argv[], char** envp) {
          registerShutdownTask([] {
              ...
              ::killOps();
              ::shellHistoryDone();
          });
          ...
          if (shellGlobalParams.files.size() == 0 && shellGlobalParams.script.empty())
              shellGlobalParams.runShell = true;
          ...
          if (shellGlobalParams.runShell) {
              ...
              shellHistoryInit();
              while (1) {...}
      
              shellHistoryDone();
          }
          ...
      }
      

      As a result: if a non-interactive mongo shell aborts, it will try to save history and report error [main] Error saving history file: FileOpenFailed: Unable to open() file : No such file or directory because the history file name is empty.

      I understand that mongo shell will continue working and this error message can actually serve as an indication that the non-interactive mongo shell aborts. However, I found some were confused by the error. Therefore, I'd suggest either move registering ::shellHistoryDone() as a shutdown task into the interactive loop if (shellGlobalParams.runShell) or skip linenoiseHistorySave in shellHistoryDone() if the history file path is empty.

      Regards,
      Lungang

            Assignee:
            backlog-server-stm Backlog - Server Tooling and Methods (STM) (Inactive)
            Reporter:
            lungang.fang@mongodb.com Lungang Fang
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: