-
Notifications
You must be signed in to change notification settings - Fork 530
Expand file tree
/
Copy pathpyodideSandboxManager.ts
More file actions
393 lines (366 loc) · 13.3 KB
/
Copy pathpyodideSandboxManager.ts
File metadata and controls
393 lines (366 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import CodebridgeRegistry from '@codebridge/CodebridgeRegistry';
import ConsoleManager from '@codebridge/Console/ConsoleManager';
import {
getErrorMessage,
getImageMessage,
getSystemError,
getSystemMessage,
} from '@codebridge/Console/MessageHelpers';
import Lab2Registry from '@cdo/apps/lab2/Lab2Registry';
import {setAndSaveSource} from '@cdo/apps/lab2/redux/lab2ProjectReduxThunks';
import {
setCodeEnvironmentError,
setHasError,
setLoadedCodeEnvironment,
} from '@cdo/apps/lab2/redux/systemRedux';
import {MultiFileSource, ProjectFile} from '@cdo/apps/lab2/types';
import {ConsoleSignalType} from '@cdo/apps/miniApps/neighborhood/constants';
import Neighborhood from '@cdo/apps/miniApps/neighborhood/Neighborhood';
import pythonlabI18n from '@cdo/apps/pythonlab/locale';
import {getStore} from '@cdo/apps/redux';
import {getInnerEnvironment} from '@cdo/apps/util/codeprojectsPreviewOrigin';
import {getPreviewDomain} from '@cdo/apps/util/sandboxedPreviewDomain';
import {createUuid} from '@cdo/apps/utils';
import {
parseMessageToNeighborhoodSignal,
parseErrorMessage,
} from './pythonHelpers/messageHelpers';
import {MessageTag} from './pythonHelpers/patches';
import {handleTheaterMedia} from './pythonHelpers/theaterMedia';
import {
FromPyodideSandboxMessage,
ToPyodideSandboxMessage,
} from './sandbox/constants';
import {PyodideMessage} from './types';
let callbacks: {[key: string]: (event: PyodideMessage) => void} = {};
const appName = 'pythonlab';
let lastInputId = '';
let outputToNeighborhood = false;
let directLogsToDevConsole = false;
let loadedMessageHandlers = false;
let sandboxServiceWorkerUnavailable = false;
let isValidationRun = false;
const getMessageHandlers = (
consoleManager: ConsoleManager | null,
neighborhood: Neighborhood | null,
outputToNeighborhood: boolean
) => {
if (outputToNeighborhood && neighborhood) {
loadedMessageHandlers = true;
return {
writeConsoleMessage: (line: string) =>
neighborhood.handleSignal({
value: ConsoleSignalType.CONSOLE_LOG,
detail: line,
}),
writePartialLine: (partialLine: string) =>
neighborhood.handleSignal({
value: ConsoleSignalType.PARTIAL_LOG,
detail: partialLine,
}),
};
} else if (consoleManager) {
loadedMessageHandlers = true;
return {
writeConsoleMessage:
consoleManager.writeConsoleMessage.bind(consoleManager),
writePartialLine: consoleManager.writePartialLine.bind(consoleManager),
};
} else {
loadedMessageHandlers = false;
return {
writeConsoleMessage: (message: string) => console.log(message),
writePartialLine: (message: string) => console.log(message),
};
}
};
let {writeConsoleMessage, writePartialLine} = getMessageHandlers(
CodebridgeRegistry.getInstance().getConsoleManager(),
CodebridgeRegistry.getInstance().getNeighborhood(),
false
);
// The pyodide worker runs inside a hidden iframe on our dedicated "preview"
// subdomain, shared with Web Lab. This is a separate base domain to ensure
// the worker never has access to the user's `studio.code.org` cookies/session.
// See "apps/src/pythonlab/sandbox/pyodideSandboxWorkerManager.ts"
// and "apps/src/pythonlab/README.md" for the full architecture.
const getSandboxOrigin = () => {
const {subdomain, port} = getInnerEnvironment();
return `${
location.protocol
}//pyodide-sandbox.preview.${subdomain}${getPreviewDomain()}${port}`;
};
// Resolved on first use rather than at module load, then pinned so the iframe
// src and the postMessage origin checks can't diverge if the DCDO-driven
// preview domain changes mid-session.
let cachedSandboxOrigin: string | undefined;
const sandboxOrigin = () => (cachedSandboxOrigin ??= getSandboxOrigin());
// How long to wait for the sandbox iframe to report ready before telling the user
// their network is blocking it. A slow but working sandbox will clear the message if
// the sandbox eventually reports ready.
const SANDBOX_READY_TIMEOUT_MS = 15000;
const SANDBOX_UNREACHABLE_MESSAGE =
'Your browser may be blocking the setup of Python Lab. You may need to ' +
'adjust your firewall settings. See the technical requirements page ' +
'(https://code.org/educate/it) for which site(s) you need ' +
'to unblock. If you need assistance, please reach out to support@code.org.';
const handlePyodideMessage = (data: PyodideMessage) => {
const {type, id, message, gif} = data;
const onSuccess = callbacks[id];
const neighborhood = CodebridgeRegistry.getInstance().getNeighborhood();
if (!loadedMessageHandlers) {
const messageHandlers = getMessageHandlers(
CodebridgeRegistry.getInstance().getConsoleManager(),
neighborhood,
false
);
writeConsoleMessage = messageHandlers.writeConsoleMessage;
writePartialLine = messageHandlers.writePartialLine;
}
switch (type) {
case 'sysout':
case 'syserr':
// Write messages to the dev console if the flag is set.
// We set this flag if we are either loading pyodide or loading packages,
// to avoid showing students confusing loading messages.
if (directLogsToDevConsole) {
console.log(message);
break;
}
// We currently treat sysout and syserr the same, but we may want to
// change this in the future. Test output goes to syserr by default.
if (message.startsWith(MessageTag.MATPLOTLIB_IMG)) {
// This is a matplotlib image, so we need to append it to the output
const image = message.slice(MessageTag.MATPLOTLIB_IMG.length + 1);
writeConsoleMessage(getImageMessage(image));
break;
}
if (message.startsWith(MessageTag.NEIGHBORHOOD_SIGNAL)) {
if (neighborhood) {
// Parse message string to NeighborhoodSignal.
const data = parseMessageToNeighborhoodSignal(message);
neighborhood.handleSignal(data);
}
break;
}
if (message.includes(MessageTag.INPUT_PROMPT)) {
const prompt = message.replace(MessageTag.INPUT_PROMPT, '');
writePartialLine(prompt);
break;
}
writeConsoleMessage(message);
break;
case 'run_complete': {
// Write a blank line to the console if we are not on a neighborhood level (which handles
// this for us).
if (!outputToNeighborhood) {
writeConsoleMessage('');
}
delete callbacks[id];
onSuccess(data);
break;
}
case 'updated_source':
getStore().dispatch(setAndSaveSource(message));
break;
case 'error':
getStore().dispatch(setHasError(true));
if (message.includes(MessageTag.INPUT_FAILED)) {
writeConsoleMessage(getErrorMessage(pythonlabI18n.inputFailed()));
break;
}
writeConsoleMessage(getErrorMessage(parseErrorMessage(message, false)));
break;
case 'system_error':
getStore().dispatch(setHasError(true));
writeConsoleMessage(
getSystemError(parseErrorMessage(message, true), appName)
);
Lab2Registry.getInstance()
.getMetricsReporter()
.logError('Python Lab System Code Error', undefined, {message});
break;
case 'internal_error':
Lab2Registry.getInstance()
.getMetricsReporter()
.logError('Python Lab Internal Error', undefined, {message});
break;
case 'load_failed':
Lab2Registry.getInstance()
.getMetricsReporter()
.logError('Failed to load packages', undefined, {message});
break;
case 'loading_pyodide':
directLogsToDevConsole = true;
getStore().dispatch(setLoadedCodeEnvironment(false));
break;
case 'loaded_pyodide':
directLogsToDevConsole = false;
getStore().dispatch(setLoadedCodeEnvironment(true));
if (message && parseInt(message)) {
Lab2Registry.getInstance()
.getMetricsReporter()
.reportLoadTime('PythonLab.PyodideLoadTime', parseInt(message));
}
break;
case 'loading_packages':
directLogsToDevConsole = true;
break;
case 'loaded_packages':
directLogsToDevConsole = false;
break;
case 'theater_media':
// Only show theater output if this is not a validation run.
if (gif && !isValidationRun) {
handleTheaterMedia(gif);
}
break;
default:
console.warn(
`Unknown message type ${type} with message ${message} from pyodideWorker.`
);
break;
}
};
const setUpPyodideSandbox = () => {
callbacks = {};
const unreachableTimeout = setTimeout(() => {
getStore().dispatch(setCodeEnvironmentError(SANDBOX_UNREACHABLE_MESSAGE));
Lab2Registry.getInstance()
.getMetricsReporter()
.logWarning(
`Pyodide sandbox at ${sandboxOrigin()} did not report ready within ${SANDBOX_READY_TIMEOUT_MS}ms`
);
}, SANDBOX_READY_TIMEOUT_MS);
const readyPromise = new Promise<void>(resolve => {
window.addEventListener('message', event => {
// The sandbox iframe is the only origin we should ever trust messages from.
if (event.origin !== sandboxOrigin()) {
return;
}
switch (event.data?.type) {
case FromPyodideSandboxMessage.READY:
clearTimeout(unreachableTimeout);
getStore().dispatch(setCodeEnvironmentError(null));
resolve();
break;
case FromPyodideSandboxMessage.SERVICE_WORKER_UNAVAILABLE:
sandboxServiceWorkerUnavailable = true;
Lab2Registry.getInstance()
.getMetricsReporter()
.logWarning('Service worker unavailable');
break;
case FromPyodideSandboxMessage.SERVICE_WORKER_REGISTRATION_FAILED:
sandboxServiceWorkerUnavailable = true;
Lab2Registry.getInstance()
.getMetricsReporter()
.logError('Failed to register input service worker', undefined, {
error: event.data.error,
});
break;
case FromPyodideSandboxMessage.AWAITING_INPUT:
lastInputId = event.data.id;
break;
default:
handlePyodideMessage(event.data as PyodideMessage);
break;
}
});
});
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.src = sandboxOrigin();
document.body.appendChild(iframe);
return {iframe, readyPromise};
};
const {iframe: pyodideSandboxIframe, readyPromise: pyodideSandboxReadyPromise} =
setUpPyodideSandbox();
const canSupportInput = () => {
return !sandboxServiceWorkerUnavailable;
};
const asyncRun = (() => {
let id = ''; // identify a Promise
return async (
script: string,
source: MultiFileSource,
validationFile?: ProjectFile,
shouldOutputToNeighborhood?: boolean
) => {
id = createUuid();
// Make sure the sandbox iframe has loaded and is ready to receive messages.
await pyodideSandboxReadyPromise;
// Reset error state
getStore().dispatch(setHasError(false));
outputToNeighborhood = !!shouldOutputToNeighborhood;
isValidationRun = !!validationFile;
const consoleManager = CodebridgeRegistry.getInstance().getConsoleManager();
const neighborhood = CodebridgeRegistry.getInstance().getNeighborhood();
const messageHandlers = getMessageHandlers(
consoleManager,
neighborhood,
outputToNeighborhood
);
writeConsoleMessage = messageHandlers.writeConsoleMessage;
writePartialLine = messageHandlers.writePartialLine;
return new Promise<PyodideMessage>(onSuccess => {
callbacks[id] = onSuccess;
pyodideSandboxIframe.contentWindow?.postMessage(
{
type: ToPyodideSandboxMessage.RUN,
python: script,
id,
source,
validationFile,
},
sandboxOrigin()
);
});
};
})();
const restartPyodideIfProgramIsRunning = () => {
// Only report stopping if the user was shown a running program. That outlasts
// the run itself for some programs -- the neighborhood keeps animating after
// its callbacks resolve -- so isRunning decides whether there was anything to stop.
// We send via the console manager rather than the message handler because the neighborhood
// stops processing messages on stop, and we want to always show this to the user.
if (getStore().getState().lab2System.isRunning) {
const consoleManager = CodebridgeRegistry.getInstance().getConsoleManager();
consoleManager?.writeConsoleMessage(
getSystemMessage(pythonlabI18n.programStopped(), appName)
);
consoleManager?.writeConsoleMessage('');
}
// Only restart if there are pending callbacks, as that means the sandbox is currently
// running a program.
if (Object.keys(callbacks).length > 0) {
callbacks = {};
pyodideSandboxIframe.contentWindow?.postMessage(
{type: ToPyodideSandboxMessage.RESTART_WEB_WORKER},
sandboxOrigin()
);
Lab2Registry.getInstance()
.getMetricsReporter()
.incrementCounter('PythonLab.PyodideRestarted');
}
};
const sendInput = (value: string): void => {
if (!canSupportInput()) {
return;
}
if (lastInputId === '') {
console.error('Worker not awaiting input');
return;
}
// Send the input value down to the sandbox, which forwards it to its own
// registered input service worker.
pyodideSandboxIframe.contentWindow?.postMessage(
{
type: ToPyodideSandboxMessage.SENDING_INPUT,
value,
id: lastInputId,
},
sandboxOrigin()
);
lastInputId = '';
};
export {asyncRun, restartPyodideIfProgramIsRunning, sendInput};