Skip to content

Commit 4697254

Browse files
committed
Make cleanup more robust
1 parent 9665def commit 4697254

2 files changed

Lines changed: 48 additions & 5 deletions

File tree

CP5/active_plugins/cpforeign/zmq_server.ipynb

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,25 @@
3535
"DENIED = \"Denied\""
3636
]
3737
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": null,
41+
"metadata": {},
42+
"outputs": [],
43+
"source": [
44+
"def cleanup(context, socket):\n",
45+
" print(\"destroying existing context\")\n",
46+
" if socket:\n",
47+
" socket.close()\n",
48+
" if context:\n",
49+
" context.term()\n",
50+
" # destroy is more destructive\n",
51+
" # doesn't require sockets closed first\n",
52+
" # may leave them hanging if managed by other threads\n",
53+
" context.destroy()\n",
54+
" print(\"socket closed\", socket.closed, \"context closed\", context.closed)\n"
55+
]
56+
},
3857
{
3958
"cell_type": "code",
4059
"execution_count": null,
@@ -43,11 +62,20 @@
4362
"source": [
4463
"context = zmq.Context()\n",
4564
"socket = context.socket(zmq.PAIR)\n",
46-
"socket.copy_threshold = 0\n",
65+
"#socket.copy_threshold = 0\n",
4766
"b = socket.bind(SOCKET_ADDR)\n",
4867
"b"
4968
]
5069
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": null,
73+
"metadata": {},
74+
"outputs": [],
75+
"source": [
76+
"cleanup(context, socket)"
77+
]
78+
},
5179
{
5280
"cell_type": "code",
5381
"execution_count": null,
@@ -345,6 +373,15 @@
345373
"source": [
346374
"# socket.send_string(\"Cancel\")"
347375
]
376+
},
377+
{
378+
"cell_type": "code",
379+
"execution_count": null,
380+
"metadata": {},
381+
"outputs": [],
382+
"source": [
383+
"cleanup(context, socket)"
384+
]
348385
}
349386
],
350387
"metadata": {

CP5/active_plugins/runforeignnb.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,23 +114,29 @@ def run(self, workspace):
114114
def do_server_handshake(self):
115115
def cleanup():
116116
LOGGER.debug("destroying existing context")
117-
if self.server_socket:
117+
if self.server_socket is not None:
118118
self.server_socket.close()
119+
LOGGER.debug(f"socket closed: {self.server_socket.closed}")
119120
self.server_socket = None
120-
if self.context:
121+
if self.context is not None:
121122
self.context.term()
123+
# destroy is more destructive
124+
# doesn't require sockets closed first
125+
# may leave them hanging if managed by other threads
122126
self.context.destroy()
127+
LOGGER.debug(f"context closed: {self.context.closed}")
128+
self.context = None
123129

124130
port = str(self.server_port.value)
125131
domain = "localhost"
126132
socket_addr = f"tcp://{domain}:{port}"
127133

128-
if self.context:
134+
if self.context is not None or self.server_socket is not None:
129135
cleanup()
130136

131137
self.context = zmq.Context()
132138
self.server_socket = self.context.socket(zmq.PAIR)
133-
self.server_socket.copy_threshold = 0
139+
#self.server_socket.copy_threshold = 0
134140

135141
LOGGER.debug(f"connecting to {socket_addr}")
136142

0 commit comments

Comments
 (0)