OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-cogserver commits / 850b68a

Python: remove global state
850b68a905765f2f792d404c46e3b3625c6a00f3
by Linas Vepstas <linasvepstas@gmail.com> · 2025-12-14T23:39:07.000Z

Changed files (2) +16 -67

opencog/cogserver/python/cogserver.py +11 -35
……@@ -27,10 +27,6 @@ if _servernode_lib is None:
2727from opencog.type_constructors import get_thread_atomspace, FloatValue, VoidValue, Predicate
2828from opencog.atomspace import types
2929
30# Global server state
31_server_handle = None
32_server_running = False
33
3430def start_cogserver(console_port=17001, web_port=18080, mcp_port=18888,
3531 enable_console=True, enable_web=True, enable_mcp=True):
3632 """Start the CogServer with the specified configuration.
……@@ -47,14 +43,8 @@ def start_cogserver(console_port=17001, web_port=18080, mcp_port=18888,
4743 CogServerNode: The CogServerNode atom that was created.
4844
4945 Raises:
50 RuntimeError: If server is already running.
5146 ValueError: If invalid ports are specified.
5247 """
53 global _server_handle, _server_running
54
55 if _server_running:
56 raise RuntimeError("CogServer is already running")
57
5848 # Validate ports
5949 for port, pname in [(console_port, "console"), (web_port, "web"), (mcp_port, "mcp")]:
6050 if not (1 <= port <= 65535):
……@@ -70,42 +60,28 @@ def start_cogserver(console_port=17001, web_port=18080, mcp_port=18888,
7060
7161 # Get the thread atomspace and add the CogServerNode to it
7262 atomspace = get_thread_atomspace()
73 _server_handle = atomspace.add_node(types.CogServerNode, name)
63 server_handle = atomspace.add_node(types.CogServerNode, name)
7464
7565 if effective_console != 17001:
7666 key = Predicate("*-telnet-port-*")
77 atomspace.set_value(_server_handle, key, FloatValue(float(effective_console)))
67 atomspace.set_value(server_handle, key, FloatValue(float(effective_console)))
7868 if effective_web != 18080:
7969 key = Predicate("*-web-port-*")
80 atomspace.set_value(_server_handle, key, FloatValue(float(effective_web)))
70 atomspace.set_value(server_handle, key, FloatValue(float(effective_web)))
8171 if effective_mcp != 18888:
8272 key = Predicate("*-mcp-port-*")
83 atomspace.set_value(_server_handle, key, FloatValue(float(effective_mcp)))
73 atomspace.set_value(server_handle, key, FloatValue(float(effective_mcp)))
8474
8575 # Start all servers (this also starts the server thread)
86 atomspace.set_value(_server_handle, Predicate("*-start-*"), VoidValue())
76 atomspace.set_value(server_handle, Predicate("*-start-*"), VoidValue())
8777
88 # Mark as initialized
89 _server_running = True
78 return server_handle
9079
91 return _server_handle
80def stop_cogserver(server_node):
81 """Stop the specified CogServer.
9282
93def stop_cogserver():
94 """Stop the running CogServer.
95
96 Returns:
97 bool: True if server was stopped, False if server was not running.
83 Args:
84 server_node: The CogServerNode to stop.
9885 """
99 global _server_handle, _server_running
100
101 if not _server_running or _server_handle is None:
102 return False
103
104 # Stop the server
10586 atomspace = get_thread_atomspace()
106 atomspace.set_value(_server_handle, Predicate("*-stop-*"), VoidValue())
107
108 _server_handle = None
109 _server_running = False
110
111 return True
87 atomspace.set_value(server_node, Predicate("*-stop-*"), VoidValue())
tests/cython/test_cogserver.py +5 -32
……@@ -46,7 +46,7 @@ class TestCogServer(unittest.TestCase):
4646 """Ensure server is stopped before each test."""
4747 global _current_server
4848 if _current_server is not None and is_server_running(_current_server):
49 stop_cogserver()
49 stop_cogserver(_current_server)
5050 _current_server = None
5151 # Small delay to ensure clean state
5252 time.sleep(0.1)
……@@ -55,7 +55,7 @@ class TestCogServer(unittest.TestCase):
5555 """Ensure server is stopped after each test."""
5656 global _current_server
5757 if _current_server is not None and is_server_running(_current_server):
58 stop_cogserver()
58 stop_cogserver(_current_server)
5959 _current_server = None
6060 # Small delay to ensure clean shutdown
6161 time.sleep(0.1)
……@@ -73,8 +73,7 @@ class TestCogServer(unittest.TestCase):
7373 "Server should be running after start")
7474
7575 # Stop the server
76 result = stop_cogserver()
77 self.assertTrue(result, "stop_cogserver should return True")
76 stop_cogserver(_current_server)
7877
7978 # Check server is stopped
8079 self.assertFalse(is_server_running(_current_server),
……@@ -102,27 +101,10 @@ class TestCogServer(unittest.TestCase):
102101 time.sleep(0.5)
103102
104103 # Stop the server
105 stop_cogserver()
104 stop_cogserver(_current_server)
106105 self.assertFalse(is_server_running(_current_server),
107106 "Server should be stopped")
108107
109 def test_double_start_error(self):
110 """Test that starting server twice raises error."""
111 global _current_server
112
113 # Start server
114 _current_server = start_cogserver(console_port=17553, enable_web=False, enable_mcp=False)
115 self.assertTrue(is_server_running(_current_server), "Server should be running")
116
117 # Try to start again - should raise RuntimeError
118 with self.assertRaises(RuntimeError) as context:
119 start_cogserver()
120
121 self.assertIn("already running", str(context.exception))
122
123 # Clean up
124 stop_cogserver()
125
126108 def test_invalid_port_error(self):
127109 """Test that invalid port raises error."""
128110 # Try invalid port - should raise ValueError
……@@ -132,14 +114,6 @@ class TestCogServer(unittest.TestCase):
132114 self.assertIn("Invalid", str(context.exception))
133115 self.assertIn("port", str(context.exception))
134116
135 def test_stop_when_not_running(self):
136 """Test stopping server when not running."""
137 # Try to stop - should return False
138 result = stop_cogserver()
139 self.assertFalse(result,
140 "stop_cogserver should return False when not running")
141
142
143117 def test_multiple_start_stop_cycles(self):
144118 """Test multiple start/stop cycles."""
145119 global _current_server
……@@ -153,8 +127,7 @@ class TestCogServer(unittest.TestCase):
153127 f"Server should be running on cycle {i}")
154128
155129 # Stop server
156 result = stop_cogserver()
157 self.assertTrue(result, f"Server should stop on cycle {i}")
130 stop_cogserver(_current_server)
158131 self.assertFalse(is_server_running(_current_server),
159132 f"Server should be stopped on cycle {i}")
160133