Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: deflake CI by ignoring finished WASM thread records (#9914)

Péter Gyarmati0dbc230e6a2e
diff --git a/marimo/_runtime/_wasm/_concurrency/_state.py b/marimo/_runtime/_wasm/_concurrency/_state.py
index 4a1db8edbf..67cadecb6c 100644
--- a/marimo/_runtime/_wasm/_concurrency/_state.py
+++ b/marimo/_runtime/_wasm/_concurrency/_state.py
@@ -82,7 +82,10 @@ def _reserved_thread_ids() -> set[int]:
current = current_identity()
if current is not None:
_add_thread_ids(reserved, current)
- for thread in live_threads:
+ for thread in list(live_threads):
+ if not thread.is_alive():
+ live_threads.discard(thread)
+ continue
_add_thread_ids(reserved, thread)
state = patch_state_value
diff --git a/tests/_runtime/test_wasm_threading.py b/tests/_runtime/test_wasm_threading.py
index 4b089b819d..ce21136f22 100644
--- a/tests/_runtime/test_wasm_threading.py
+++ b/tests/_runtime/test_wasm_threading.py
@@ -108,6 +108,38 @@ def test_wasm_threading_synthetic_ids_skip_real_thread_ids(
unpatch()
+def test_wasm_threading_synthetic_ids_ignore_finished_thread_records(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ real_ident = threading.get_ident()
+ real_native_id = getattr(threading, "get_native_id", threading.get_ident)()
+ expected_ident = max(real_ident, real_native_id) + 1000
+
+ with _mock_pyodide_with_run_sync():
+ from marimo._runtime._wasm._concurrency import _state
+
+ unpatch = install_wasm_concurrency_shims()
+ try:
+ stale_thread = _state.ThreadIdentity()
+ stale_thread._ident = expected_ident
+ stale_thread._native_id = expected_ident
+ _state.live_threads.add(stale_thread)
+ monkeypatch.setattr(
+ _state,
+ "_IDENTS",
+ iter([real_ident, real_native_id, expected_ident]),
+ )
+
+ thread = threading.Thread(name="synthetic", target=lambda: None)
+ thread.start()
+ thread.join(timeout=1)
+
+ assert thread.ident == expected_ident
+ assert stale_thread not in _state.live_threads
+ finally:
+ unpatch()
+
+
def test_wasm_threading_local_dict_is_read_only() -> None:
with _mock_pyodide_with_run_sync():
unpatch = install_wasm_concurrency_shims()