[spambayes-dev] Prevent multiple servers on Windows
mhammond at skippinet.com.au
Thu Sep 18 01:02:16 EDT 2003
We have touched a little on this before, and I suspect that at least one of
Richie's problems with the binary could be explained by having the proxy
running multiple times.
I propose that for Windows, we hack in a simple mutex to prevent *any* of
the server based apps from starting if any of them are already running. We
can fix it later :)
Does anyone object to the following patch? There will have to be changes to
pop3proxy_tray/service, but they are trivial.
RCS file: /cvsroot/spambayes/spambayes/scripts/sb_server.py,v
retrieving revision 1.4
diff -u -r1.4 sb_server.py
--- sb_server.py 13 Sep 2003 05:10:49 -0000 1.4
+++ sb_server.py 18 Sep 2003 04:55:17 -0000
@@ -118,6 +118,10 @@
newsoft = min(hard, max(soft, 1024*2048))
resource.setrlimit(resource.RLIMIT_STACK, (newsoft, hard))
+# exception may be raised if we are already running and check such things.
# number to add to STAT length for each msg to fudge for spambayes headers
HEADER_SIZE_FUDGE_FACTOR = 512
@@ -726,6 +730,10 @@
+ except AttributeError:
state = State()
@@ -743,6 +751,29 @@
+ # If we can, prevent multiple servers from running at the same time.
+ # This can be refactored later if other platforms ever want to do
+ # similar.
+ if sys.platform.startswith("win"):
+ import win32event, win32api, winerror
+ # ideally, the mutex name could include either the username, or
+ # the munged path to the INI file - this would mean we would
+ # multiple starts so long as they weren't for the same user.
+ # However, as of now, the service version is likely to start as
+ # a different user, so a single mutex is best for now.
+ mutex_name = "SpamBayesServer"
+ hmutex = win32event.CreateMutex(None, True, mutex_name)
+ if win32api.GetLastError()==winerror.ERROR_ALREADY_EXISTS:
+ raise AlreadyRunningException
+ # remember the handle, but no real need to explicitly close it
+ # when done, as it will die with the process.
+ state.windows_mutex = hmutex
+ except ImportError:
+ # no win32all - no worries, just start
# Do whatever we've been asked to do...
@@ -808,7 +839,12 @@
print get_version_string("POP3 Proxy")
print "and engine %s.\n" % (get_version_string(),)
+ except AlreadyRunningException:
+ print "ERROR: The proxy is already running on this machine."
+ print "Please stop the existing proxy and try again"
if 0 <= len(args) <= 2:
# Normal usage, with optional server name and port number.
More information about the spambayes-dev