Repository navigation
Description
@anntzer @tacaswell
Hi! I have just tested the master branch on Windows and found that there is indeed a problem with the QSocketNotifier (it appeared here: #13306 (comment)). Previously, on that Windows machine, I was running my old code with QAbstractSocket which was fine, and did not test the newest version with QSocketNotifier there (my fault, sorry). I have tested on Linux, and there is no such problem there.
The code to reproduce on Windows (in Jupyter):
%matplotlib qt
from matplotlib import pyplot as plt
plt.plot([1,2])
plt.pause(.1)
plt.pause(.1)
plt.pause(.1)The problem seems to be that if socketpair generates a socket with the same fileno value as it generated in the previous pause() call, the QSocketNotifier unexpectedly fires and hangs here:
| sn.activated.connect(lambda *args: rsock.recv(1)) |
because nothing was actually written to the
wakeup_fd. I don't understand how that can be.
Workaround fix:
rsock.set_blocking(False)
@sn.activated.connect
def on_signal(*args):
try:
signal = rsock.recv(1) # clear the socket to re-arm the notifier
except BlockingIOError:
pass # false-trigger, nothing on rsockWhat should we do with this? Is there a better solution?
Originally posted by @vdrhtc in #13306 (comment)
Activity
QuLogic commentedon Aug 30, 2021
Is it the current
QSocketNotifierthat fires, or the previous one? Are we just missing some cleanup?vdrhtc commentedon Aug 31, 2021
@QuLogic

Here how it looks like, I don't think it is the same QSocketNotifier, it is a different object every time. But false-trigger only happens if the
filenois re-used (1092 in this case, for 1028 nothing happens). So maybe it is necessary to clear the rsock somehow.QuLogic commentedon Sep 1, 2021
Does something like this work?
It should print out that no sockets are ready. Windows support was only added in 3.5, but maybe there's a bug?
Maybe we should call
socket.shutdownon the sockets to force them to be disabled, even though we do close them:QuLogic commentedon Sep 1, 2021
Also, does reusing the existing socket pair work?
vdrhtc commentedon Sep 1, 2021
Nothing unexpected in the first test despite occasional re-uses:

Second test also works:

If I remove the
del snline, then there is a warning about multiple notifiers for the same socket in the terminal, and no activation happens on both notifiers.Calling
does not help.
QuLogic commentedon Sep 1, 2021
Actually, for that second test, maybe we should run the event loop in between?
vdrhtc commentedon Sep 16, 2021
@QuLogic For some reason, the event loop does not exit after 5s and then the program hangs...
I've obtained the previous results by running %matplotlib qt first (it probably starts the event loop correctly by itself)
timhoffm commentedon Oct 1, 2021
Is this fixed by #20907?
QuLogic commentedon Oct 1, 2021
It should be.