Repository navigation
Open
Labels
bugSomething isn't workingSomething isn't working
Description
Summary
When a turn ends and the orchestration is still waiting, the runtime drops the generator. CPython then closes it: it raises GeneratorExit at the yield. So finally blocks and except BaseException blocks in the orchestration run. This happens once per turn, not once per orchestration.
Where
- The Rust guard calls the dispose function when the turn ends:
duroxide-python/src/handlers.rs
Lines 94 to 110 in 0a29c32
impl Drop for OrchestrationInvokeGuard { fn drop(&mut self) { ORCHESTRATION_CTXS.lock().remove(&self.instance_id); let Some(gen_id) = self.gen_id.take() else { return; }; // Call dispose synchronously via GIL if let Err(e) = Python::with_gil(|py| -> PyResult<()> { self.dispose_fn.call1(py, (gen_id.to_string(),))?; Ok(()) }) { eprintln!("Warning: dispose_fn failed: {e}"); } } } dispose_generatordrops the last reference to the generator:duroxide-python/python/duroxide/driver.py
Lines 127 to 135 in 0a29c32
def dispose_generator(id_str: str) -> str: """Dispose of a generator. Called from Rust when the orchestration handler finishes or is dropped. """ gen_id = int(id_str) _generators.pop(gen_id, None) _last_tasks.pop(gen_id, None) return "ok"
Reproduction
runs = []
@runtime.register_orchestration("WithFinally")
def with_finally(ctx, _):
try:
yield ctx.schedule_timer(50)
yield ctx.schedule_timer(50)
yield ctx.schedule_timer(50)
return "done"
finally:
runs.append(1)The orchestration completes once. len(runs) is 4, one per turn.
Impact
- Cleanup code in a
finallyblock runs many times, and at points where the orchestration has not finished. - A
yieldinside thefinallyblock raisesRuntimeError: generator ignored GeneratorExitduring the close. - Context calls made from that code (
set_kv_value,set_custom_status,trace_*) are dropped, because the native context is removed before the generator is closed.
Suggested fix
CPython closes a suspended generator when it is freed, so the SDK cannot fully prevent this. It can do two things:
- Document that orchestration code must not depend on
try/finallyaround ayield. - Set a flag on the context before the generator is dropped, and expose it (for example
ctx.is_disposed). Cleanup code can then skip its work, and context calls after that point can raise a clear error.
Tracked in microsoft/duroxide#55.
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working
Activity