Skip to content

Instantly share code, notes, and snippets.

@eiri
Created March 16, 2016 13:42
Show Gist options
  • Save eiri/43292219faa67487eccb to your computer and use it in GitHub Desktop.
Save eiri/43292219faa67487eccb to your computer and use it in GitHub Desktop.
Does erlang's timers survive a death of their hatcher process?
Erlang/OTP 17 [erts-6.4.1.2] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V6.4.1.2  (abort with ^G)
1> c(zombie_timer).
{ok,zombie_timer}
2> zombie_timer:main().
-. start timer {1458135323982793,#Ref<0.0.0.79>} from <0.40.0>
-. loop is <0.40.0>
1. <0.40.0>
2. undefined
-. loop is <0.42.0>
3. <0.42.0> = <0.42.0>
ok
oh, hi!
3> 'they surely do'.
'they surely do'
4> 
-module (zombie_timer).
-export([main/0, loop/2, hello/1]).
main() ->
Ref = make_ref(),
First = spawn(?MODULE, loop, [Ref, true]),
%% register is not a magic,
%% the signal needs time to get through
timer:sleep(5),
io:format("1. ~p~n", [whereis(?MODULE)]),
exit(First, kill),
timer:sleep(5),
io:format("2. ~p~n", [whereis(?MODULE)]),
Second = spawn(?MODULE, loop, [Ref, false]),
timer:sleep(5),
io:format("3. ~p = ~p~n", [Second, whereis(?MODULE)]),
ok.
loop(Ref, true) ->
TRef = timer:apply_after(1000, ?MODULE, hello, [Ref]),
io:format("-. start timer ~p from ~p~n", [TRef, self()]),
loop(Ref);
loop(Ref, false) ->
loop(Ref).
loop(Ref) ->
Me = self(),
io:format("-. loop is ~p~n", [Me]),
case whereis(?MODULE) of
Me -> ok;
undefined -> register(?MODULE, Me)
end,
receive
{Ref, hello} ->
io:format("oh, hi!~n");
Else ->
io:format("# ~p~n", [Else]),
loop(Ref)
after
infinity -> end_of_universe
end.
hello(Ref) ->
?MODULE ! {Ref, hello}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment