Repository navigation
gh-157548: Emscripten: take a signal from the signal buffer in one step #157553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| On Emscripten, a signal written to the signal buffer while the interpreter | ||
| was clearing it is no longer lost. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2945,6 +2945,7 @@ incref_decref_delayed(PyObject *self, PyObject *op) | |
|
|
||
| #ifdef __EMSCRIPTEN__ | ||
| #include "emscripten.h" | ||
| #include "pycore_emscripten_signal.h" // Py_EMSCRIPTEN_SIGNAL_HANDLING | ||
|
|
||
| EM_JS(int, emscripten_set_up_async_input_device_js, (void), { | ||
| let idx = 0; | ||
|
|
@@ -2972,6 +2973,36 @@ emscripten_set_up_async_input_device(PyObject *self, PyObject *Py_UNUSED(ignored | |
| Py_RETURN_FALSE; | ||
| } | ||
| } | ||
|
|
||
| EM_JS(void, emscripten_set_signal_buffer_js, (int signum, int shared), { | ||
| Module._Py_saved_signal_buffer = Module.Py_EmscriptenSignalBuffer; | ||
| Module.Py_EmscriptenSignalBuffer = shared | ||
| ? new Uint8Array(new SharedArrayBuffer(1)) | ||
| : new Uint8Array(1); | ||
| Module.Py_EmscriptenSignalBuffer[0] = signum; | ||
| }); | ||
|
|
||
| EM_JS(int, emscripten_restore_signal_buffer_js, (void), { | ||
| const value = Module.Py_EmscriptenSignalBuffer[0]; | ||
| Module.Py_EmscriptenSignalBuffer = Module._Py_saved_signal_buffer; | ||
| delete Module._Py_saved_signal_buffer; | ||
| return value; | ||
| }); | ||
|
|
||
| static PyObject * | ||
| emscripten_check_signal_buffer(PyObject *self, PyObject *args) | ||
| { | ||
| int signum, shared; | ||
| if (!PyArg_ParseTuple(args, "ip", &signum, &shared)) { | ||
| return NULL; | ||
| } | ||
| int handling = Py_EMSCRIPTEN_SIGNAL_HANDLING; | ||
| emscripten_set_signal_buffer_js(signum, shared); | ||
| Py_EMSCRIPTEN_SIGNAL_HANDLING = 1; | ||
| _Py_CheckEmscriptenSignals(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe use CheckInterrupt() here since it should work and it's public api.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll look into it!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed that using a public API rather than an internal would be preferable. |
||
| Py_EMSCRIPTEN_SIGNAL_HANDLING = handling; | ||
| return PyLong_FromLong(emscripten_restore_signal_buffer_js()); | ||
| } | ||
| #endif | ||
|
|
||
| static PyObject * | ||
|
|
@@ -3383,6 +3414,7 @@ static PyMethodDef module_functions[] = { | |
| GET_NEXT_DICT_KEYS_VERSION_METHODDEF | ||
| #ifdef __EMSCRIPTEN__ | ||
| {"emscripten_set_up_async_input_device", emscripten_set_up_async_input_device, METH_NOARGS}, | ||
| {"emscripten_check_signal_buffer", emscripten_check_signal_buffer, METH_VARARGS}, | ||
| #endif | ||
| {"simple_pending_call", simple_pending_call, METH_O}, | ||
| {"set_vectorcall_nop", set_vectorcall_nop, METH_O}, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this loop at all? It looks to me like the signal is triggered immediately by
_Py_CheckEmscriptenSignals();. Also,emscripten_check_signal_buffer()turns signal handling back off so it certainly won't trigger later...Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I confess I just added the test because I was ashamed to contribute without one, but I also think it's not necessary. There's no way to reasonably test the thread stuff, so it's mostly a placeholder. I would be +1 for removing it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think having some check that this mechanism at least sort of works is not a bad thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a comment that it's not a realistic test of the feature but it at least executes the code path to make sure it's not completely broken.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed having a basic test of the functionality is worth it; trying to manufacture a multi-threaded race condition isn't. I think dropping the redundant loop, and flagging the testing limitation in a comment will be sufficient.