Merge lp:~cmiller/desktopcouch/find-port-runtimeerror into lp:desktopcouch

Proposed by Chad Miller
Status: Merged
Approved by: Tim Cole
Approved revision: no longer in the revision history of the source branch.
Merged at revision: not available
Proposed branch: lp:~cmiller/desktopcouch/find-port-runtimeerror
Merge into: lp:desktopcouch
Diff against target: 83 lines (+21/-21)
2 files modified
desktopcouch/__init__.py (+11/-10)
desktopcouch/start_local_couchdb.py (+10/-11)
To merge this branch: bzr merge lp:~cmiller/desktopcouch/find-port-runtimeerror
Reviewer Review Type Date Requested Status
Stuart Langridge (community) Approve
Eric Casteleijn (community) Approve
Review via email: mp+15109@code.launchpad.net

Commit message

There is a period between when couchdb has written its PID file and when it is really listening on a socket for connections. Our code assumes that writing its PID file is a signal that it's ready to work, but it is not. Now, we loop on looking for the socket, instead of interpreting missing as an error.

To post a comment you must log in.
105. By Manuel de la Peña

Ensures that the method that test for the presence of the record does not just check if the field is present but ensures the value is correct.

Revision history for this message
Eric Casteleijn (thisfred) wrote :

Looks good, tests pass.

review: Approve
Revision history for this message
Stuart Langridge (sil) wrote :

Much nicer than before. Good catch.

review: Approve
106. By Chad Miller

There is a period between when couchdb has written its PID file and when it is really listening on a socket for connections. Our code assumes that writing its PID file is a signal that it's ready to work, but it is not. Now, we loop on looking for the socket, instead of interpreting missing as an error.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'desktopcouch/__init__.py'
2--- desktopcouch/__init__.py 2009-11-18 18:43:41 +0000
3+++ desktopcouch/__init__.py 2009-11-21 00:00:31 +0000
4@@ -57,11 +57,16 @@
5
6 return pid
7
8-def find_port__linux(pid=None):
9- if pid is None:
10- pid = find_pid()
11-
12- proc_dir = "/proc/%s" % (pid,)
13+def find_port__linux(pid=None, ctx=local_files.DEFAULT_CONTEXT):
14+ """This returns a valid port or raises a RuntimeError exception. It never
15+ returns anything else."""
16+ if pid is None:
17+ pid = find_pid(start_if_not_running=True, ctx=ctx)
18+
19+ if pid is None:
20+ raise RuntimeError("Have no PID to use to look up port.")
21+
22+ proc_dir = "/proc/%d" % (pid,)
23
24 # enumerate the process' file descriptors
25 fd_dir = os.path.join(proc_dir, 'fd')
26@@ -104,12 +109,8 @@
27 if match is not None:
28 port = str(int(match.group(1), 16))
29 break
30+
31 if port is None:
32- log.error("Unable to find listening port")
33- log.error("Looked at the following processes for inode %s:" % inode_subexp)
34- fp = open(os.path.join(proc_dir, 'net', 'tcp'))
35- log.error(fp.read())
36- fp.close()
37 raise RuntimeError("Unable to find listening port")
38
39 return port
40
41=== modified file 'desktopcouch/start_local_couchdb.py'
42--- desktopcouch/start_local_couchdb.py 2009-11-20 20:35:16 +0000
43+++ desktopcouch/start_local_couchdb.py 2009-11-21 00:00:31 +0000
44@@ -63,8 +63,6 @@
45 def create_ini_file(port="0", ctx=local_files.DEFAULT_CONTEXT):
46 """Write CouchDB ini file if not already present"""
47
48- print "ini file is at", ctx.file_ini
49-
50 if os.path.exists(ctx.file_ini):
51 # load the username and password from the keyring
52 try:
53@@ -220,20 +218,21 @@
54 exit(1)
55
56 # give the process a chance to start
57- for timeout in (0.1, 0.1, 0.2, 0.5, 1, 3, 5):
58+ for timeout in (0.4, 0.1, 0.1, 0.2, 0.5, 1, 3, 5):
59 pid = read_pidfile(ctx=ctx)
60 if pid is not None and process_is_couchdb(pid):
61 break
62- print "...waiting for couchdb to start..."
63 time.sleep(timeout)
64
65- # loop for a number of times until the port has been found, this
66- # has to be done to prevent a race condition when trying to retrieve
67- # the port number is slow hardware (ie netbooks)
68- for timeout in (0.1, 0.1, 0.2, 0.5, 1, 3, 5):
69- port = desktopcouch.find_port(pid=pid)
70- if port is not None: break
71- print "...waiting for couchdb to start..."
72+ # Loop for a number of times until the port has been found, this
73+ # has to be done because there's a slice of time between PID being written
74+ # and the listening port being active.
75+ for timeout in (0.1, 0.1, 0.2, 0.5, 1, 3, 5, 8):
76+ try:
77+ port = desktopcouch.find_port(pid=pid, ctx=ctx) # only returns valid port
78+ break
79+ except RuntimeError, e:
80+ pass
81 time.sleep(timeout)
82
83 ctx.ensure_files_not_readable()

Subscribers

People subscribed via source and target branches