Skip to content
Open
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
10 changes: 10 additions & 0 deletions ipykernel/heartbeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import errno
import socket
from concurrent.futures import Future
from pathlib import Path
from threading import Thread

Expand Down Expand Up @@ -46,6 +47,7 @@ def __init__(self, context, addr=None, *, curve_publickey=None, curve_secretkey=
addr = ("tcp", localhost(), 0)
Thread.__init__(self, name="Heartbeat")
self.context = context
self._bind_future: Future[None] = Future()
self.transport, self.ip, self.port = addr
self.original_port = self.port
self._curve_publickey = curve_publickey
Expand Down Expand Up @@ -104,6 +106,11 @@ def _bind_socket(self):
else:
return

def start(self):
"""Start the heartbeat thread and wait for its socket to bind."""
super().start()
self._bind_future.result()

def run(self):
"""Run the heartbeat thread."""
self.name = "Heartbeat"
Expand All @@ -119,6 +126,9 @@ def run(self):
self.socket.close()
raise

self.addr = (self.ip, self.port)
self._bind_future.set_result(None)

while True:
try:
zmq.device(zmq.QUEUE, self.socket, self.socket)
Expand Down
3 changes: 2 additions & 1 deletion ipykernel/kernelapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,10 @@ def init_heartbeat(self):
curve_publickey=self.curve_publickey,
curve_secretkey=self.curve_secretkey,
)
# Binding may select a new port if the original candidate was taken.
self.heartbeat.start()
self.hb_port = self.heartbeat.port
self.log.debug("Heartbeat REP Channel on port: %i", self.hb_port)
self.heartbeat.start()

def close(self):
"""Close zmq sockets in an orderly fashion"""
Expand Down
Loading