Skip to content

Conversation

@lpil
Copy link
Member

@lpil lpil commented Feb 28, 2025

Following on from gleam-lang/erlang#71

@karlsson
Copy link
Contributor

Calls in gleam shell to sys:get_state / get_status for a given actor process works fine, but the observer tool (erlang) blocks these calls since in checks if attributes '$ancestors' and '$initial_call' exist. Adding those at actor init makes observer work as expected. I attach a patch.
observer_support.patch.txt
observer3

@karlsson
Copy link
Contributor

Calls in gleam shell to sys:get_state / get_status for a given actor process works fine, but the observer tool (erlang) blocks these calls since in checks if attributes '$ancestors' and '$initial_call' exist. Adding those at actor init makes observer work as expected.

Actually calling erlang function proc_lib:spawn_link/1 instead of process.spawn will add the attributes to the process registry, so no need for any special function in that case. But maybe this shall be fixed later once 1.0 is out.

diff --git a/src/gleam/otp/actor.gleam b/src/gleam/otp/actor.gleam
index e732964..c8ae979 100644
--- a/src/gleam/otp/actor.gleam
+++ b/src/gleam/otp/actor.gleam
@@ -572,8 +572,7 @@ pub fn start(
   let ack_subject = process.new_subject()
   let self = process.self()
 
-  let child =
-    process.spawn(fn() { initialise_actor(builder, self, ack_subject) })
+  let child = spawn(fn() { initialise_actor(builder, self, ack_subject) })
 
   let monitor = process.monitor(child)
   let selector =
@@ -611,6 +610,9 @@ pub fn start(
   }
 }
 
+@external(erlang, "proc_lib", "spawn_link")
+fn spawn(start_fun: fn() -> ExitReason) -> Pid
+
 /// Send a message over a given channel.
 ///
 /// This is a re-export of `process.send`, for the sake of convenience.

@lpil
Copy link
Member Author

lpil commented Apr 24, 2025

@karlsson Yes for sure! I was just talking about this with the team.

Replicating the special process functionality was great for proving out the gleam_erlang design and ensuring it was capable enough to implement things this complex, but I think there's a lot to gain now by using higher level Erlang building blocks, specifically proc_lib. Better compatibility as you say, and also we won't get caught up by interface changes, such as the addition of aliases.

@lpil lpil marked this pull request as ready for review May 6, 2025 17:09
@lpil lpil marked this pull request as draft May 6, 2025 17:15
@lpil lpil marked this pull request as ready for review May 15, 2025 19:25
@karlsson
Copy link
Contributor

Applying this:

diff --git a/src/gleam_otp_external.erl b/src/gleam_otp_external.erl
index 13d0546..bf733e7 100644
--- a/src/gleam_otp_external.erl
+++ b/src/gleam_otp_external.erl
@@ -43,12 +43,17 @@ convert_system_message({system, {From, Ref}, Request}) when is_pid(From) ->
     end.
 
 process_status({status_info, Module, Parent, Mode, DebugState, State}) ->
-    Data = [
-        get(), Mode, Parent, DebugState,
-        [{header, "Status for Gleam process " ++ pid_to_list(self())},
-         {data, [{'Status', Mode}, {'Parent', Parent}, {'State', State}]}]
-    ],
-    {status, self(), {module, Module}, Data}.
+     Data = [
+         get(), Mode, Parent, DebugState,
+         [{header, "Status for Gleam process " ++ pid_to_list(self())},
+           {data, [
+             {"Gleam behaviour", Module},
+             {"Status", Mode},
+             {"Parent", Parent}]},
+          {data, [{"State", State}]}
+         ]
+     ],
+     {status, self(), {module, Module}, Data}.
 
 application_stopped() ->
     ok.

Will give you this:
status2
instead of this:
status1
Note that you get a Gleam behavior!

@lpil
Copy link
Member Author

lpil commented May 16, 2025

Love it @karlsson ! Would you like to make a pull request?

@lpil lpil merged commit 15e2ec1 into main May 16, 2025
1 check passed
@lpil lpil deleted the redux branch May 16, 2025 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants