Skip to content
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
4 changes: 4 additions & 0 deletions ipykernel/kernelapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,10 @@ def close(self):
self.log.debug("Closing iopub channel")
self.iopub_thread.stop()
self.iopub_thread.close()
elif self.iopub_socket is not None and not self.iopub_socket.closed:
# The iopub port could not be bound, so no IOPub thread owns the socket.
# Left open, it would block context.term() below.
self.iopub_socket.close()
if self.control_thread and self.control_thread.is_alive():
self.log.debug("Closing control thread")
self.control_thread.stop()
Expand Down
18 changes: 18 additions & 0 deletions tests/test_kernelapp.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import atexit
import json
import os
import socket
import threading
import time
from unittest.mock import patch

import pytest
import zmq
from jupyter_client import KernelManager
from jupyter_client.kernelspec import KernelSpecManager
from jupyter_core.paths import secure_write
Expand All @@ -30,6 +33,21 @@ def test_init_ipc_socket():
app.close()


def test_close_after_iopub_port_in_use():
app = IPKernelApp(transport="tcp")
with socket.socket() as taken:
taken.bind((app.ip, 0))
taken.listen(1)
app.iopub_port = taken.getsockname()[1]
with pytest.raises(zmq.ZMQError):
app.init_sockets()
atexit.unregister(app.close)
closing = threading.Thread(target=app.close, daemon=True)
closing.start()
closing.join(timeout=10)
assert not closing.is_alive(), "close() is still waiting for the zmq context to terminate"


def test_blackhole():
app = IPKernelApp()
app.no_stderr = True
Expand Down
Loading