Skip to content

Commit dacd6a8

Browse files
committed
Cleanup websocket lists and references to instance components, on close
1 parent ffbce10 commit dacd6a8

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

vuejspython.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def call_watcher(o, k):
157157
if callable(watcher):
158158
watcher(getattr(o, k))
159159

160-
all = defaultdict(lambda: set([]))
160+
all = defaultdict(lambda: set())
161161
KEY_ATOMIC = '_v_ATOMIC'
162162
async def broadcast_update(id, k, v):
163163
a = all[id].copy()
@@ -182,16 +182,28 @@ def next_instance_id():
182182
return str(_previd[0])
183183

184184
async def handleClient(websocket, path):
185-
# TODO: cleanup g_instances (on socket disconnect at least)
185+
inited = None
186+
def cleanup():
187+
if inited is None: return
188+
if id != 'ROOT' and id in g_instances:
189+
del g_instances[id]
190+
if websocket in all[inited]:
191+
all[inited].remove(websocket)
192+
if len(all[inited]) == 0:
193+
del all[inited]
186194
try:
187195
while True:
196+
print({k:len(all[k]) for k in all})
188197
comm = await websocket.recv()
189198
if comm == 'INIT' or comm == 'INFO':
190199
clss_name = await websocket.recv()
200+
if inited is not None:
201+
info('ERR', 'Tentative double init/info', clss_name)
191202
info('IN', comm, clss_name)
192203
if clss_name == 'ROOT':
193-
all[clss_name].add(websocket)
194204
id = clss_name
205+
all[id].add(websocket)
206+
inited = id
195207
o = g_instances[id]
196208
clss = type(o)
197209
o._v_just_schedule = False
@@ -202,6 +214,7 @@ async def handleClient(websocket, path):
202214
o = clss()
203215
id = next_instance_id()
204216
all[id].add(websocket)
217+
inited = id
205218
setattr(o, '__id', id)
206219
setup_model_object_infra(o)
207220
g_instances[id] = o
@@ -265,6 +278,7 @@ async def handleClient(websocket, path):
265278
info('ERR', 'Not a JSON value (or watcher error) for key', k, '->', v, '//', e)
266279
info_exception('ERR', ' ')
267280
except websockets.ConnectionClosed as e:
281+
cleanup()
268282
if e.code == 1001:
269283
info('END', 'disconnected')
270284
elif e.code == 1005:
@@ -273,6 +287,7 @@ async def handleClient(websocket, path):
273287
info('END', e)
274288
info_exception('ENDx')
275289
except Exception as e:
290+
cleanup()
276291
info('END', e)
277292
info_exception('ENDe')
278293
return handleClient

0 commit comments

Comments
 (0)