@@ -433,14 +433,8 @@ def _safe_hint(s):
433433 with open(idFilename, 'w') as f:
434434 f.write(zitiIdContent)
435435
436- # Load the identity file after it's been written and closed
437- try:
438- openziti.load(idFilename)
439- except Exception as e:
440- print(f"ERROR: Failed to load Ziti identity: {e}")
441- schema = generate_json_schema(zitiIdJson)
442- print(f"DEBUG: zitiId schema for troubleshooting: {json.dumps(schema, indent=2)}")
443- raise e
436+ # Defer openziti.load() until inside the monkeypatch context to keep
437+ # initialization/teardown paired and avoid double-free on shutdown.
444438
445439 # Create webhook body
446440 try:
@@ -454,13 +448,36 @@ def _safe_hint(s):
454448 data = mwb.dumpJson()
455449
456450 with openziti.monkeypatch():
451+ # Load the identity inside the context so that the same owner tears down
452+ # resources, reducing the chance of double shutdown/free.
453+ try:
454+ openziti.load(idFilename)
455+ except Exception as e:
456+ print(f"ERROR: Failed to load Ziti identity: {e}")
457+ schema = generate_json_schema(zitiIdJson)
458+ print(f"DEBUG: zitiId schema for troubleshooting: {json.dumps(schema, indent=2)}")
459+ raise e
460+
461+ session = None
462+ r = None
457463 try:
464+ session = requests.Session()
458465 print(f"Posting webhook to {url} with headers {headers} and data {data}")
459- # breakpoint()
460- r = requests.post(url, headers=headers, data=data)
466+ r = session.post(url, headers=headers, data=data)
461467 print(f"Response Status: {r.status_code}")
462468 print(r.headers)
463469 print(r.content)
464470 except Exception as e:
465471 print(f"Exception posting webhook: {e}")
466472 raise e
473+ finally:
474+ try:
475+ if r is not None:
476+ r.close()
477+ except Exception:
478+ pass
479+ try:
480+ if session is not None:
481+ session.close()
482+ except Exception:
483+ pass
0 commit comments