Skip to content

Commit

Permalink
Merge pull request #234 from appsignal/fix-scheduler-stop
Browse files Browse the repository at this point in the history
Call `scheduler.stop()` when AppSignal stops
  • Loading branch information
unflxw authored Feb 3, 2025
2 parents aff7442 + 11afe17 commit 2d05a48
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changesets/fix-scheduler-stop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: patch
type: fix
---

Fix an issue where calling `appsignal.stop()` after sending check-in events would leave a dangling thread, stopping the application from shutting down correctly.
3 changes: 3 additions & 0 deletions src/appsignal/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ def start(self) -> None:
logger.info("AppSignal not starting: no active config found")

def stop(self) -> None:
from .check_in.scheduler import scheduler

logger.info("Stopping AppSignal")
scheduler().stop()
working_dir = self._config.option("working_directory_path") or "/tmp/appsignal"
lock_path = os.path.join(working_dir, "agent.lock")
try:
Expand Down
18 changes: 17 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def test_client_inactive():
@patch("time.sleep", return_value=None)
@patch("os.kill", return_value=None)
@patch("builtins.open", new_callable=mock_open, read_data="123456;running;123\n")
def test_client_stop(mock_open, mock_kill, mock_sleep):
def test_client_stop_kills_agent(mock_open, mock_kill, mock_sleep):
client = Client(active=True, name="MyApp", push_api_key="0000-0000-0000-0000")
client.start()

Expand All @@ -116,3 +116,19 @@ def test_client_stop(mock_open, mock_kill, mock_sleep):
call(123, signal.SIGTERM),
]
)


def test_client_stop_stops_scheduler(mocker):
# use mocker to check that the `stop` method in the `_scheduler` global variable
# in `check_in.scheduler._scheduler` is called

stop_mock = mocker.patch("appsignal.check_in.scheduler._scheduler.stop")

client = Client(active=True, name="MyApp", push_api_key="0000-0000-0000-0000")
client.start()

stop_mock.assert_not_called()

client.stop()

stop_mock.assert_called_once()

0 comments on commit 2d05a48

Please sign in to comment.