Переглянути джерело

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

GitOrigin-RevId: 0c1d40bbde58d04de362586103a50b522dc14c2a
Domagoj Kriskovic 4 місяців тому
батько
коміт
cb0f87282b

+ 4 - 3
services/web/frontend/js/features/ide-react/components/editor/python/pyodide.worker.ts

@@ -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',