[erlang-questions] Possible bug in process_info ?

Tim Rath rath64@REDACTED
Tue Sep 26 04:25:16 CEST 2006


Hi,

I was playing around with a macro to log some additional detail along with a message, and found something peculiar with the arity that process_info reports.

This is not a big deal as I was only intending to use this for simple debugging purposes, but I thought someone maintaining this code might be interested.

Here's a test function that shows the behavior I found:


-module(b).
-compile(export_all).

test() ->

  S = "Foo = ~p",
  P = [bar],

  %% Correctly reports arity
  %%
  {_, {Ma, Fa, Aa} } = process_info(self(), current_function),
  io:fwrite("~w:~w/~w : " ++ S ++ "~n", [Ma, Fa, Aa] ++ P),

  %% Correctly reports arity
  %%
  Fun =
    fun() ->
      {_, {M, F, A} } = process_info(self(), current_function),
      io:fwrite("~w:~w/~w~n", [M, F, A])
    end,
  Fun(),

  %% Adds 2 to the arity ?
  %%
  Fun0 =
    fun() ->
      {_, {M, F, A} } = process_info(self(), current_function),
      io:fwrite("~w:~w/~w : " ++ S ++ "~n", [M, F, A] ++ P)
    end,
  Fun0(),

  %% Adds 2 to the arity ?
  %%
  Fun1 =
    fun(_, _, _) ->
      {_, {M, F, A} } = process_info(self(), current_function),
      io:fwrite("~w:~w/~w : " ++ S ++ "~n", [M, F, A] ++ P)
    end,
  Fun1(1, 2, 3).



And here's the output:

50> b:test().
b:test/0 : Foo = bar
b:'-test/0-fun-0-'/0
b:'-test/0-fun-1-'/2 : Foo = bar
b:'-test/0-fun-2-'/5 : Foo = bar
ok


Strangely, it appears that changing the parameters to the io:fwrite function change the arity in the previous call to process_info (but only inside a fun)

Note the effect of the following change to the code:

-module(b).
-compile(export_all).

test() ->

  S = "Foo = ~p",
  SS = ", Nick = ~p",
  P = [bar, nack],

  %% Correctly reports arity
  %%
  {_, {Ma, Fa, Aa} } = process_info(self(), current_function),
  io:fwrite("~w:~w/~w : " ++ S ++ SS ++ "~n", [Ma, Fa, Aa] ++ P),

  %% Correctly reports arity
  %%
  Fun =
    fun() ->
      {_, {M, F, A} } = process_info(self(), current_function),
      io:fwrite("~w:~w/~w~n", [M, F, A])
    end,
  Fun(),

  %% Adds 3 to the arity ?
  %%
  Fun0 =
    fun() ->
      {_, {M, F, A} } = process_info(self(), current_function),
      io:fwrite("~w:~w/~w~n", [M, F, A]),
      io:fwrite("~w:~w/~w : " ++ S ++ SS ++ "~n", [M, F, A] ++ P),
      io:fwrite("~w:~w/~w~n", [M, F, A])
    end,
  Fun0(),

  %% Adds 3 to the arity ?
  %%
  Fun1 =
    fun(_, _, _) ->
      {_, {M, F, A} } = process_info(self(), current_function),
      io:fwrite("~w:~w/~w : " ++ S ++ SS ++ "~n", [M, F, A] ++ P)
    end,
  Fun1(1, 2, 3).


Output:

61> b:test().
b:test/0 : Foo = bar, Nick = nack
b:'-test/0-fun-0-'/0
b:'-test/0-fun-1-'/3
b:'-test/0-fun-1-'/3 : Foo = bar, Nick = nack
b:'-test/0-fun-1-'/3
b:'-test/0-fun-2-'/6 : Foo = bar, Nick = nack
ok


Not really a problem for me, but I found it interesting anyway.

-Tim



More information about the erlang-questions mailing list