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

The mongod process forks before listening for connections

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 3.4.11, 3.6.1, 3.7.1
    • Affects Version/s: 3.4.9
    • Component/s: None
    • Labels:
      None
    • Fully Compatible
    • ALL
    • v3.6, v3.4
    • Hide

      Create a systemd service to test if the socket is listening:

      [Unit]
      Type=oneshot
      Description=TestService
      After=mongod.service
      Wants=mongod.service
       
      [Service]
      ExecStart=/usr/bin/testsocket
      Restart=on-abort
       
      [Install]
      WantedBy=multi-user.target
      

      With the following script (/usr/bin/testsocket)

      #!/bin/sh
      /bin/nc localhost 27017 </dev/null >/dev/null && echo "Listening" || echo "Not listening"
      

      Stop mongod, and start the service which should start mongod first:

      [root@localhost ~]# systemctl stop mongod
      [root@localhost ~]# ps aux|egrep '[m]ongo'    
      [root@localhost ~]# systemctl start testservice
      [root@localhost ~]# systemctl status testservice
      ● testservice.service - TestService
      
      Sep 21 19:49:02 localhost.localdomain systemd[1]: Started TestService.
      Sep 21 19:49:02 localhost.localdomain systemd[1]: Starting TestService...
      Sep 21 19:49:02 localhost.localdomain testsocket[3830]: Ncat: Connection refused.
      Sep 21 19:49:02 localhost.localdomain testsocket[3830]: Not listening
      

      Add a 1 second sleep via ExecStartPre in the testservice unit file, or to the beginning of /usr/bin/testsocket and mongod is listening by the time it runs:

      [root@localhost ~]# systemctl stop mongod       
      [root@localhost ~]# ps aux|egrep '[m]ongo'    
      [root@localhost ~]# systemctl start testservice       
      [root@localhost ~]# systemctl status testservice        
      ● testservice.service - TestService
      
      Sep 21 19:52:13 localhost.localdomain systemd[1]: Started TestService.
      Sep 21 19:52:13 localhost.localdomain systemd[1]: Starting TestService...
      Sep 21 19:52:14 localhost.localdomain testsocket[3882]: Listening
      
      Show
      Create a systemd service to test if the socket is listening: [Unit] Type=oneshot Description=TestService After=mongod.service Wants=mongod.service [Service] ExecStart=/usr/bin/testsocket Restart=on-abort [Install] WantedBy=multi-user.target With the following script ( /usr/bin/testsocket ) #!/bin/sh /bin/nc localhost 27017 </dev/null >/dev/null && echo "Listening" || echo "Not listening" Stop mongod, and start the service which should start mongod first: [root@localhost ~]# systemctl stop mongod [root@localhost ~]# ps aux|egrep '[m]ongo' [root@localhost ~]# systemctl start testservice [root@localhost ~]# systemctl status testservice ● testservice.service - TestService Sep 21 19:49:02 localhost.localdomain systemd[1]: Started TestService. Sep 21 19:49:02 localhost.localdomain systemd[1]: Starting TestService... Sep 21 19:49:02 localhost.localdomain testsocket[3830]: Ncat: Connection refused. Sep 21 19:49:02 localhost.localdomain testsocket[3830]: Not listening Add a 1 second sleep via ExecStartPre in the testservice unit file, or to the beginning of /usr/bin/testsocket and mongod is listening by the time it runs: [root@localhost ~]# systemctl stop mongod [root@localhost ~]# ps aux|egrep '[m]ongo' [root@localhost ~]# systemctl start testservice [root@localhost ~]# systemctl status testservice ● testservice.service - TestService Sep 21 19:52:13 localhost.localdomain systemd[1]: Started TestService. Sep 21 19:52:13 localhost.localdomain systemd[1]: Starting TestService... Sep 21 19:52:14 localhost.localdomain testsocket[3882]: Listening
    • Build Ready

      When starting mongod as a systemd service, the process forks before mongod has opened the socket and starts listening for connections. This can cause dependent services to fail to start, as systemd considers mongod started once the process has forked.

            Assignee:
            mathew.robinson@mongodb.com Mathew Robinson (Inactive)
            Reporter:
            mark.agarunov Mark Agarunov
            Votes:
            0 Vote for this issue
            Watchers:
            12 Start watching this issue

              Created:
              Updated:
              Resolved: