[SERVER-5457] Service install with dbpath ending in backslash results in non-runnable service. Created: 30/Mar/12  Updated: 02/Apr/15  Resolved: 02/Apr/15

Status: Closed
Project: Core Server
Component/s: Packaging
Affects Version/s: 2.0.3
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Mo Cassidy Assignee: Unassigned
Resolution: Done Votes: 0
Labels: service
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 7 SP1


Operating System: Windows
Participants:

 Description   

The following command:

c:\SitesData\survey.unifocus.com\mongo\mongod.exe --rest --replSet rsUfSurvey --dbpath C:\SitesData\survey.unifocus.com\mongodata_test27022\ --serviceName Mongo27022 --serviceDisplayName "MongoDB 27022" --serviceDescription "Instance of mongod.exe listening on TCP port 27022" --logpath C:\SitesData\survey.unifocus.com\mongo_27022.log --logappend --port 27022 --install

creates a service with a HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\MongoDB\ImagePath value of:

"c:\SitesData\survey.unifocus.com\mongo\mongod.exe" --rest  --replSet  rsUfSurvey  --dbpath  "C:\SitesData\survey.unifocus.com\mongodata_test27022\"  --logpath  "C:\SitesData\survey.unifocus.com\mongo_27022.log"  --logappend  --port  27022  --service

When atetmpting to start the service using NET START Mongo27022, the following error message is given:

The service is not responding to the control function.

If I try to manually run the ImagePath value without "--service" on the end, I get the following error:

Fri Mar 30 08:57:05 [initandlisten] exception in initAndListen: 10296 dbpath (C:\SitesData\survey.unifocus.com\mongodata_test27022"  --logpath  C:\SitesData\survey.unifocus.com\mongo_27022.log  --logappend  --port  27022) does not exist, terminating

Evidently, the backslash at the end of the dbpath is being treated as an escape sequence and preventing the quotes (which were added automatically by --install !) from closing correctly.

A suitable workaround is obviously to not put a backslash character at the end of the dbpath, but it definitely had me scratching my head for a bit!



 Comments   
Comment by Tad Marshall [ 31/Mar/12 ]

Thanks, Mo, those are good suggestions. I'll update the wiki page as you suggested. And you are right that the quotes that are causing the problem are ones that we added, and in the case you gave there were no spaces in the dbpath to make the quotes required. It wouldn't have made any difference if you had added quotes around your dbpath: the Windows (and C runtime?) command line processing removes them before we see the argv list and we add them to protect spaces (without checking to see if spaces are there to require them). If the dbpath you specify includes spaces, then you HAVE to provide them, otherwise we won't be given the dbpath as a single argument. (The same applies to other filespecs that include spaces, though only directories could legally include a backslash at the end). So, we could potentially only quote file and directory specifications that include spaces and we could even strip a trailing backslash from the end of directory strings like dbpath.

Thanks for the detailed and easily reproducible bug report ... we'll at least document it, and maybe we can make it better!

Comment by Mo Cassidy [ 30/Mar/12 ]

Thanks for the detailed explanation. That's an interesting problem. I can understand why you wouldn't want to fix it, but perhaps some warning on http://www.mongodb.org/display/DOCS/Windows+Service would be helpful. Is mongo adding the quotes itself? If it didn't add quotes automatically (and left it up the user to add quotes if there are spaces in the parameter values), then that might cut down significantly on the people affected by this, which is almost as good as building in a workaround to the problem

Comment by Tad Marshall [ 30/Mar/12 ]

Thanks for the report. This is an interesting bug. This is evidently a "feature" of the Windows CommandLineToArgvW function, which is apparently used by the C runtime to prepare the argc and argv parameters that are passed to mongod.exe. The documentation describes the processing of backslashes and quotes on the command line:

CommandLineToArgvW has a special interpretation of backslash characters when they are followed by a quotation mark character ("), as follows:
2n backslashes followed by a quotation mark produce n backslashes followed by a quotation mark.
(2n) + 1 backslashes followed by a quotation mark again produce n backslashes followed by a quotation mark.
n backslashes not followed by a quotation mark simply produce n backslashes.

See http://msdn.microsoft.com/en-us/library/windows/desktop/bb776391(v=vs.85).aspx for the source of this explanation. For additional discussion, see Raymond Chen's blog: http://blogs.msdn.com/b/oldnewthing/archive/2010/09/17/10063629.aspx .

When I step through the code in the debugger, I see that we get, as a single parameter (argv[5] in my test) 'C:\data\db" --port 27022 --service' ... note the embedded quote. This also explains why the service never starts ... the "--service" parameter that tells mongod.exe to talk to the Windows Service Control Manager has been "swallowed", moved into the dbpath parameter.

The only "fix" I can imagine for this is to do the parsing of the command line options ourselves and not use the argc/argv parameters we are given. This seems pretty clumsy and a heavy-handed solution and so we probably won't do this.

Thanks for letting us know about this. As you figured out, not ending your path with a backslash is recommended.

Generated at Thu Feb 08 03:08:57 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.