diff --git a/robot-docs/discord.md b/robot-docs/discord.md index 07ee4983d..4fbe7b5e4 100644 --- a/robot-docs/discord.md +++ b/robot-docs/discord.md @@ -89,7 +89,7 @@ A DM reaction event names no one, so the process asks Discord who it was once an The page and `discord.status()` show one state with a detail: -- `disabled`: the bot is disabled, or the extension is off globally. +- `disabled`: the bot is disabled, or the extension is off globally. Lines its closed process still had in flight are dropped, so they cannot change this state. - `connecting` / `online` / `reconnecting`: as the process reports. - `reconnecting` with "the bot process exited …; restarting in Ns": the process died; the daemon restarts it after a doubling pause (1 s up to 60 s), reset by the next `online`. - `failed`: a fatal close (4004 bad token, 4010–4013, 4014 Message Content intent not enabled). The daemon does not restart it until the bot's token or enabled flag changes. diff --git a/src/albedo/harness/extensions/discord/manager.gleam b/src/albedo/harness/extensions/discord/manager.gleam index 17f261a1c..b58b5849c 100644 --- a/src/albedo/harness/extensions/discord/manager.gleam +++ b/src/albedo/harness/extensions/discord/manager.gleam @@ -302,12 +302,14 @@ fn disabled(live: Live, detail: String) -> Live { ) } +/// Closes the process and drops its lines still in flight: launched +/// generations start at 1, so none matches 0. fn stop(live: Live) -> Live { case live.pid { Some(pid) -> port.close(pid) None -> Nil } - Live(..live, pid: None) + Live(..live, pid: None, generation: 0) } fn launch(state: State, bot: String, live: Live) -> State { diff --git a/test/e2e/albedo_discord_reader_probe.erl b/test/e2e/albedo_discord_reader_probe.erl new file mode 100644 index 000000000..46469c909 --- /dev/null +++ b/test/e2e/albedo_discord_reader_probe.erl @@ -0,0 +1,20 @@ +%% Hold a discord bot's port reader so a status line is still in flight when the manager disables the bot. +-module(albedo_discord_reader_probe). +-export([hold/0, release/0]). + +hold() -> + Manager = whereis(albedo_discord_manager), + {monitors, Monitors} = process_info(Manager, monitors), + [Reader] = [P || {process, P} <- Monitors], + Parent = self(), + Holder = spawn(fun() -> + true = erlang:suspend_process(Reader), + Parent ! held, + receive release -> ok end + end), + register(albedo_discord_reader_holder, Holder), + receive held -> <<"held">> after 5000 -> error(not_held) end. + +release() -> + albedo_discord_reader_holder ! release, + <<"released">>. diff --git a/test/e2e/discord_test.py b/test/e2e/discord_test.py index 96588236d..281b55ca9 100644 --- a/test/e2e/discord_test.py +++ b/test/e2e/discord_test.py @@ -12,7 +12,8 @@ import unittest import urllib.error from discord_fake_support import FakeDiscord -from harness import Albedo, Provider, exclusive, python, text, wait_until +from harness import ROOT, Albedo, Provider, exclusive, python, text, wait_until +from inspect_support import Inspection def local_zone(): @@ -74,6 +75,7 @@ class DiscordTests(unittest.TestCase): def prepare(app): app.env["ALBEDO_DISCORD_API"] = self.fake.api_url + app.daemon.env["ALBEDO_INSPECT"] = "1" app.write_extensions({"enabled": {"discord": True}}) self.app = Albedo(self.provider, protocol="responses", prepare=prepare) @@ -629,6 +631,26 @@ class DiscordTests(unittest.TestCase): patched = json.load(response) self.assertEqual((patched["enabled"], patched["state"]), (False, "disabled")) + def test_a_line_in_flight_cannot_undo_a_disable(self): + bot = self.create_bot() + probe = Inspection(self.app, ROOT / "test/e2e/albedo_discord_reader_probe.erl") + probe.call("hold") + try: + # The helper reports "reconnecting"; the held reader forwards it only + # after the manager has already disabled the bot. + self.fake.drop_connection() + time.sleep(1) + with self.app.api( + self.route, + {"revision": bot["revision"], "enabled": False}, + method="PATCH", + ) as response: + self.assertEqual(json.load(response)["state"], "disabled") + finally: + probe.call("release") + time.sleep(1) + self.assertEqual(self.bot()["state"], "disabled") + if __name__ == "__main__": unittest.main()