ipython / ipython/ipykernel

Async widget methods deadlock due to on_msg not being called

オープン
#646 コメント 11 件 リアクション 1 件 担当者 0 名 GitHub で見る
主要言語
Python
スター
734
フォーク
412
平均マージ
1日 5時間
マージ済み PR(30日)
8

説明

We are developing a Jupyter widget which makes it possible to [embed interactive Unfolded maps into notebooks](https://docs.unfolded.ai/jupyter):

![image](https://user-images.githubusercontent.com/351828/115682557-de2d7200-a355-11eb-934f-ab1e20dd6455.png)

Our widget is embedded via an iframe and so the communications with the JavaScript side are asynchronous. Some of the functions we want to add to the widget need to return data asynchronously. To communicate between Jupyter and JS we are using widget's `send` and `on_msg` methods. Our methods return `future`s. It works fine - the messages are being sent and received, but only if we do it in two steps:

![image](https://user-images.githubusercontent.com/351828/115682504-cfdf5600-a355-11eb-8e36-bb22e7dc310a.png)

When we are trying to use `await` so that we can have the results and do something with them within one cell, the execution is blocked forever:

![image](https://user-images.githubusercontent.com/351828/115682627-f0a7ab80-a355-11eb-9c72-6dd27f36e5c8.png)

We tried to investigate the cause. It turns out that the callback we register with `widget.on_msg` only gets called once the execution of the cell is finished. Hence, if we wait until we obtain the result of the calculation within the same cell where the method was called, we end up in a deadlock.

So far we've had no success in finding a workaround, despite trying many things. For instance, here we run the communication in a separate thread and wait until the query response is added to a blocking queue:
![image](https://user-images.githubusercontent.com/351828/115683846-2305d880-a357-11eb-9662-bce8e60f0270.png)

Calling the above function fails (after the 2 seconds timeout) with an error saying that the queue is empty:
![image](https://user-images.githubusercontent.com/351828/115683897-2f8a3100-a357-11eb-8119-65ba873f20ba.png)
And we don't see the `_receive` function being called.

However, if we check the queue immediately after getting this error, we can see that the queue is not empty - the response was added to it, apparently, *_just after_* the error caused the cell execution to be cancelled:
![image](https://user-images.githubusercontent.com/351828/115684127-606a6600-a357-11eb-85ed-2395e6d0aff5.png)

It might be the same issue as this one: https://github.com/ipython/ipython/issues/12786
but I am not completely sure. I tried installing and running the kernel with the fix from [this PR](https://github.com/ipython/ipykernel/pull/589), but I was getting errors related to traitlet messaging when trying to instantiate our widget.

コントリビューションガイド

コントリビューションガイドを開く

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。