Skip to content

Commit

Permalink
Bugfix: Close heartbeat thread more reliably (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
m4wh6k authored Jan 10, 2023
1 parent 6151836 commit 1f06045
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.11
0.8.12
4 changes: 2 additions & 2 deletions src/boardwalk/cli_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,8 +634,8 @@ def unmutex_boardwalkd_workspace():
ctx.call_on_close(unmutex_boardwalkd_workspace)

# Send heartbeats in background
heartbeat_coroutine = boardwalkd_client.heartbeat_keepalive_connect()
ctx.call_on_close(heartbeat_coroutine.cancel)
heartbeat_quit = boardwalkd_client.heartbeat_keepalive_connect()
ctx.call_on_close(heartbeat_quit.set)


def update_host_facts_in_local_state(host: Host, workspace: Workspace):
Expand Down
15 changes: 11 additions & 4 deletions src/boardwalkd/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import json
import logging
import socket
import threading
import time
import webbrowser
from collections import deque
Expand Down Expand Up @@ -257,9 +258,14 @@ def workspace_post_heartbeat(self, workspace_name: str):
else:
raise e

def workspace_heartbeat_keepalive(self, workspace_name: str) -> None:
def workspace_heartbeat_keepalive(
self, workspace_name: str, quit: threading.Event
) -> None:
"""Tries to post a heartbeat to the server every 5 seconds"""
while True:
if quit.is_set():
logging.debug("Heartbeat keepalive thread closing")
return
try:
self.workspace_post_heartbeat(workspace_name)
except (
Expand All @@ -275,12 +281,13 @@ def workspace_heartbeat_keepalive(self, workspace_name: str) -> None:

def workspace_heartbeat_keepalive_connect(
self, workspace_name: str
) -> concurrent.futures.Future[None]:
) -> threading.Event:
"""Starts a background thread to post heartbeats to the server so it
knows when a client is alive"""
executor = concurrent.futures.ThreadPoolExecutor()
future = executor.submit(self.workspace_heartbeat_keepalive, workspace_name)
return future
quit = threading.Event()
executor.submit(self.workspace_heartbeat_keepalive, workspace_name, quit)
return quit

def workspace_post_event(
self,
Expand Down

0 comments on commit 1f06045

Please sign in to comment.