fix: ensure original write method is correctly restored after code execution

GitOrigin-RevId: 0c1d40bbde58d04de362586103a50b522dc14c2a
This commit is contained in:
Domagoj Kriskovic
2026-04-09 11:35:45 +02:00
committed by Copybot
parent 7d032e73d6
commit cb0f87282b

View File

@@ -127,13 +127,13 @@ async function handleRunCode(msg: RunCodeRequest) {
},
})
const fs = instance.FS
const originalWrite = fs.write as PyodideFS['write']
try {
const fs = instance.FS
if (msg.files.length > 0) {
syncProjectFiles(fs, msg.files)
}
const originalWrite = fs.write as PyodideFS['write']
fs.write = ((...args: Parameters<PyodideFS['write']>) => {
const [stream] = args
// Only surface writes to the synced project workspace, not Pyodide internals.
@@ -144,7 +144,7 @@ async function handleRunCode(msg: RunCodeRequest) {
writtenPaths.add(stream.path)
}
return originalWrite(...args)
return originalWrite.call(fs, ...args)
}) as PyodideFS['write']
const result = await instance.runPythonAsync(msg.code)
@@ -167,6 +167,7 @@ async function handleRunCode(msg: RunCodeRequest) {
requestId: msg.id,
})
} finally {
fs.write = originalWrite
const outputs = [...writtenPaths].sort()
self.postMessage({
type: 'run-code-result',