Bug #54104 WB should not use a fixed tcp port for its internal docs http server
Submitted: 31 May 2010 13:16 Modified: 11 Jun 2010 10:47
Reporter: Hartmut Holzgraefe Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Workbench Severity:S4 (Feature request)
Version:WB 5.2.21rc OS:Linux
Assigned to: Alfredo Kojima CPU Architecture:Any

[31 May 2010 13:16] Hartmut Holzgraefe
Description:
Currently the integrated docs webserver listens on a fixed port 8811,
and is not prepared to handle the situation that this port may already
be taken (by another service, by another WB instance run by the same
or a different user on the same OS instance, ...)

As the port number is passed on to the appropriate client automatically
it does not need to be a fixed number (ok, bookmarks on doc items would
not work)

How to repeat:
Have something else listen on port 8811, then start WB and try to access the integrated documentation

Suggested fix:
Use a random user local port (will add a patch for that in a bit), or make the port number configurable instead of using the fixed/compiled in port 8811.
[31 May 2010 13:18] Hartmut Holzgraefe
=== modified file 'plugins/wb.doclib/wb_doclib_grt.py'
--- plugins/wb.doclib/wb_doclib_grt.py	2010-05-29 10:07:07 +0000
+++ plugins/wb.doclib/wb_doclib_grt.py	2010-05-29 10:35:49 +0000
@@ -58,9 +58,30 @@
         App.get().set_status_text("Doc Library Opened")
 
 
-def run_server(datadir, server_port, ready_event):
+def get_free_port(bind = ''):
+        import socket
+        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        s.bind((bind,0)) # binding to port zero will actually bind to a free user port
+        port = s.getsockname()[1]
+        s.close()
+        return port
+
+
+def run_server(datadir, ready_event):
     import mysqldoclib
-    mysqldoclib.serve_docs(server_port, bind='localhost', datadir=datadir, ready_event=ready_event)
+    import pprint
+    global server_port;
+    
+    retries = 10 
+
+    while server_port is None and --retries > 0:
+        try:
+            server_port = get_free_port() 
+            # there is a slight chance that the port gets taken before we use it again
+            # which is the main reason for the retry loop
+            mysqldoclib.serve_docs(server_port, bind='localhost', datadir=datadir, ready_event=ready_event)
+        except:
+            server_port = None
     
 
 server_port = None
@@ -89,11 +110,11 @@
     
     if server_port is None:
         ready_event = Event()
-        server_port = 8811
 
         #datadir = "./modules/data/DocLibrary/"
         datadir = os.path.join(app.get_resource_path(""), "modules/data/DocLibrary")
-        thread.start_new_thread(run_server, (datadir, server_port, ready_event))
+
+        thread.start_new_thread(run_server, (datadir, ready_event))    
 
         # wait up to 1s for the doclib server to start
         ready_event.wait(1)
[2 Jun 2010 12:14] Alfredo Kojima
Patch was applied, thanks for it!
[9 Jun 2010 13:01] Johannes Taxacher
fix confirmed in repository. WB now starts the doc-pages-server on random free port on localhost
[11 Jun 2010 10:47] Tony Bedford
An entry has been added to the 5.2.23 changelog:

The integrated docs webserver listened on a fixed port - 8811, and was not able to handle the situation where this port may be used by another service, such as another MySQL Workbench instance.