aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2025-11-15 21:53:26 -0500
committerelioat <elioat@tilde.institute>2025-11-15 21:53:26 -0500
commita29bd0e9499b7b32e51f3dbd554303ada7b078fc (patch)
tree9f5093d53ca2efb031b7fa5d0ca2ccbe52889cec
parent1ec6fdbe7fbb3fec37f99b063e779d5f375cd319 (diff)
downloadtour-a29bd0e9499b7b32e51f3dbd554303ada7b078fc.tar.gz
Implement graceful shutdown with /quit and /leave commands
- Wire up exit_vm() call in /quit and /leave handler to properly terminate the VM - Add exit_vm() function to gossip_utils.erl that calls erlang:halt(0) - Users can now cleanly exit the application with /quit or /leave - Process properly terminates instead of hanging
-rw-r--r--gleam/gossip/src/gossip_utils.erl6
-rw-r--r--gleam/gossip/src/p2p_peer.gleam19
2 files changed, 17 insertions, 8 deletions
diff --git a/gleam/gossip/src/gossip_utils.erl b/gleam/gossip/src/gossip_utils.erl
index 23ba0fe..8bd93c9 100644
--- a/gleam/gossip/src/gossip_utils.erl
+++ b/gleam/gossip/src/gossip_utils.erl
@@ -1,5 +1,5 @@
-module(gossip_utils).
--export([read_line/0, read_line_no_echo/0, send_to_pid/2, binary_to_atom_utf8/1, rpc_send_message/3, pid_to_subject/1, is_valid_subject/1, erlang_send/2, erlang_receive_timeout/1, extract_pid_from_subject/1, sum_char_codes/1, get_timestamp/0, get_unix_timestamp/0]).
+-export([read_line/0, read_line_no_echo/0, send_to_pid/2, binary_to_atom_utf8/1, rpc_send_message/3, pid_to_subject/1, is_valid_subject/1, erlang_send/2, erlang_receive_timeout/1, extract_pid_from_subject/1, sum_char_codes/1, get_timestamp/0, get_unix_timestamp/0, exit_vm/0]).
-include_lib("kernel/include/inet.hrl").
@@ -172,3 +172,7 @@ get_timestamp() ->
get_unix_timestamp() ->
erlang:system_time(second).
+% Exit the Erlang VM gracefully
+exit_vm() ->
+ erlang:halt(0).
+
diff --git a/gleam/gossip/src/p2p_peer.gleam b/gleam/gossip/src/p2p_peer.gleam
index 4b8b48c..944f8d9 100644
--- a/gleam/gossip/src/p2p_peer.gleam
+++ b/gleam/gossip/src/p2p_peer.gleam
@@ -171,13 +171,13 @@ pub fn input_loop(registry_subject: process.Subject(messages.Message), name: Str
io.println("Goodbye!")
Nil
}
- "/quit" | "/leave" -> {
- // User explicitly quit - notify peers and exit
- process.send(registry_subject, messages.ClientLeave(name, registry_id))
- let _ = sleep_ms(500)
- io.println("Goodbye!")
- Nil
- }
+ "/quit" | "/leave" -> {
+ // User explicitly quit - notify peers and exit
+ process.send(registry_subject, messages.ClientLeave(name, registry_id))
+ let _ = sleep_ms(500)
+ io.println("Goodbye!")
+ exit_vm()
+ }
_ -> {
// Get current timestamp
let timestamp = get_timestamp()
@@ -272,3 +272,8 @@ fn find_remote_registry_with_retries(peer_name: String, retries_left: Int) -> pr
/// Sleep for a number of milliseconds (via Erlang timer:sleep/1)
@external(erlang, "timer", "sleep")
fn sleep_ms(milliseconds: Int) -> Nil
+
+/// Exit the Erlang VM gracefully
+@external(erlang, "gossip_utils", "exit_vm")
+fn exit_vm() -> Nil
+