Skip to content

Commit 6bcd134

Browse files
committed
Reset asyncio event loop before script execution
Adds logic to reset or replace the asyncio event loop between runs to prevent issues caused by reusing a running loop from a previous app session. This helps avoid blocking new script executions due to an already running event loop.
1 parent 04cda82 commit 6bcd134

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/serious_python_android/lib/src/cpython.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,22 @@ if threading.current_thread() is not threading.main_thread():
112112
_signalPatched = true;
113113
}
114114

115+
// Reset/replace asyncio loop between runs to avoid reusing a running loop from
116+
// a previous app session that could block new runs.
117+
const loopReset = """
118+
import asyncio
119+
try:
120+
loop = asyncio.get_event_loop()
121+
if loop.is_running():
122+
loop.stop()
123+
loop.close()
124+
asyncio.set_event_loop(asyncio.new_event_loop())
125+
except Exception as e:
126+
pass
127+
""";
128+
_pyLog("resetting asyncio loop");
129+
cpython.PyRun_SimpleString(loopReset.toNativeUtf8().cast());
130+
115131
try {
116132
if (script != "") {
117133
// run script

0 commit comments

Comments
 (0)