Skip to content
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

Fix message throttle by making a copy of the callbacks dict #3485

Merged
merged 2 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions packages/base/src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,10 @@ export class WidgetModel extends Backbone.Model {
if (this.comm !== void 0) {
if (msg.content.execution_state === 'idle') {
this._pending_msgs--;
// Sanity check for logic errors that may push this below zero.
if (this._pending_msgs < 0) {
console.error(
`Pending messages < 0 (=${this._pending_msgs}), which is unexpected`
`Jupyter Widgets message throttle: Pending messages < 0 (=${this._pending_msgs}), which is unexpected. Resetting to 0 to continue.`
);
this._pending_msgs = 0; // do not break message throttling in case of unexpected errors
}
Expand Down Expand Up @@ -553,17 +554,18 @@ export class WidgetModel extends Backbone.Model {
return '';
}
try {
callbacks.iopub = callbacks.iopub || {};
if (callbacks.iopub.previouslyUsedByJupyterWidgets === undefined) {
// Do not break other code that also wants to listen to status updates
callbacks.iopub.statusPrevious = callbacks.iopub.status;
}
// else callbacks.iopub.status is the old handler, that we should not reuse
callbacks.iopub.previouslyUsedByJupyterWidgets = true;
// Make a 2-deep copy so we don't modify the caller's callbacks object.
callbacks = {
shell: { ...callbacks.shell },
iopub: { ...callbacks.iopub },
input: callbacks.input,
};
// Save the caller's status callback so we can call it after we handle the message.
const statuscb = callbacks.iopub.status;
callbacks.iopub.status = (msg: KernelMessage.IStatusMsg): void => {
this._handle_status(msg);
if (callbacks.iopub.statusPrevious) {
callbacks.iopub.statusPrevious(msg);
if (statuscb) {
statuscb(msg);
}
};

Expand Down
8 changes: 4 additions & 4 deletions packages/base/test/src/widget_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ describe('WidgetModel', function () {
});
expect(this.widget._msg_buffer_callbacks).to.not.equals(null);
// have the comm send a status idle message
callbacks.iopub.status({
this.comm.send.lastCall.args[1].iopub.status({
content: {
execution_state: 'idle',
},
Expand All @@ -662,7 +662,7 @@ describe('WidgetModel', function () {
},
buffer_paths: [],
});
callbacks.iopub.status({
this.comm.send.lastCall.args[1].iopub.status({
content: {
execution_state: 'idle',
},
Expand All @@ -687,7 +687,7 @@ describe('WidgetModel', function () {
state: { a: 'sync test - 2' },
buffer_paths: [],
});
callbacks.iopub.status({
this.comm.send.lastCall.args[1].iopub.status({
content: {
execution_state: 'idle',
},
Expand All @@ -703,7 +703,7 @@ describe('WidgetModel', function () {
},
buffer_paths: [],
});
callbacks.iopub.status({
this.comm.send.lastCall.args[1].iopub.status({
content: {
execution_state: 'idle',
},
Expand Down