thread waiting for input survives the end of execution
- Dominant language
- Python
- Stars
- 734
- Forks
- 412
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 8
Description
If a thread asks for input after the end of execution, it will crash as expected. If it asks for input before the end of execution, it will hang in a strange state that will crash ipykernel as soon as another input request is sent.
The offending line is: `self.session.recv(self.stdin_socket, 0)` in `kernelbase.Kernel._input_request`. If `stdin_socket.close()` is called before `recv`, it will crash. If `stdin_socket.close()` is called after `self.session.recv(self.stdin_socket, 0)`, it will hang forever.
### Ask after end
```python
In [1]: import threading
...: import time
...:
...: def getinput():
...: time.sleep(1)
...: print(input('thread:'))
...:
...: thread = threading.Thread(target=getinput)
...: thread.start()
...: time.sleep(0.5)
thread:Exception in thread Thread-12:
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "", line 6, in getinput
print(input('thread:'))
EOFError: EOF when reading a line
In [2]: print(input('main:'))
main:test
test
In [3]:
```
### Ask before end
```python
In [1]: import threading
...: import time
...:
...: def getinput():
...: time.sleep(0.5)
...: print(input('thread:'))
...:
...: thread = threading.Thread(target=getinput)
...: thread.start()
...: time.sleep(1)
thread:
In [2]: print(input('main:'))
main:hi
[IPKernelApp] WARNING | Invalid Message:
Traceback (most recent call last):
File "/Users/quentinpeter/pyscripts/ipykernel/ipykernel/kernelbase.py", line 884, in _input_request
ident, reply = self.session.recv(self.stdin_socket, 0)
File "/usr/local/lib/python3.7/site-packages/jupyter_client/session.py", line 835, in recv
idents, msg_list = self.feed_identities(msg_list, copy)
File "/usr/local/lib/python3.7/site-packages/jupyter_client/session.py", line 866, in feed_identities
idx = msg_list.index(DELIM)
ValueError: b'' is not in list
[IPKernelApp] WARNING | Invalid Message:
Traceback (most recent call last):
File "/Users/quentinpeter/pyscripts/ipykernel/ipykernel/kernelbase.py", line 884, in _input_request
ident, reply = self.session.recv(self.stdin_socket, 0)
File "/usr/local/lib/python3.7/site-packages/jupyter_client/session.py", line 840, in recv
raise e
File "/usr/local/lib/python3.7/site-packages/jupyter_client/session.py", line 837, in recv
return idents, self.deserialize(msg_list, content=content, copy=copy)
File "/usr/local/lib/python3.7/site-packages/jupyter_client/session.py", line 945, in deserialize
raise ValueError("Invalid Signature: %r" % signature)
ValueError: Invalid Signature: b'{}'
```
Contributor guide
Assessment
This issue has not been assessed yet.