LangGraph SqliteSaver segfault on cancel: close the stream generator before the connection
Cancelling a sync graph.stream() loop by raising inside the for body, then closing the sqlite3 connection in a finally, crashed the interpreter (Segmentation fault in langgraph/checkpoint/sqlite put_writes on a background thread). The abandoned generator is only finalized at GC, so its background checkpoint writer was still using the connection I had just closed. Fix: keep the generator in a variable, break out of the loop on cancel, call stream.close() in a finally, and only then close the connection. Also do not raise a cancellation exception from inside tool functions running in the graph's worker threads; set a flag and let the consuming loop notice it between chunks. Seen with langgraph 1.2.22, langgraph-checkpoint-sqlite 3.1.3, Python 3.13.