Erlang 19.3 crashes when getting sockname of stdin

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Erlang 19.3 crashes when getting sockname of stdin

Attila Rajmund Nohl
Hello!

I have a oneliner that prints out which erlang process is listening on
a given TCP port. Recently I upgraded from Debian 8.9 to Debian 9.2
and on Erlang/OTP 19.3.1 this oneliner no longer works. The minimal
example to reproduce:

Erlang/OTP 19 [erts-8.3.1] [source] [64-bit] [smp:2:2]
[async-threads:10] [hipe] [kernel-poll:false]

Eshell V8.3.1  (abort with ^G)
1> inet:sockname(self()).
** exception error: no function clause matching prim_inet:sockname(<0.58.0>)
2> inet:sockname(hd(erlang:ports())).
Failed to write to erl_child_setup: 9

Crash dump is being written to: erl_crash.dump...done

The (relevant?) part of the crash dump:

Current Process: <0.60.0>
Current Process State: Running
Current Process Internal State: ACT_PRIO_NORMAL | USR_PRIO_NORMAL |
PRQ_PRIO_NORMAL | ACTIVE | RUNNING | ON_HEAP_MSGQ
Current Process Program counter: 0x00007f695af4b1d0 (unknown function)
Current Process CP: 0x0000000000000000 (invalid)
Current Process Limited Stack Trace:
0x00007f6958e16048:SReturn addr 0x5C473830 (prim_inet:ctl_cmd/3 + 64)
0x00007f6958e16070:SReturn addr 0x5C45F3F8 (prim_inet:sockname/1 + 96)
0x00007f6958e16080:SReturn addr 0x5C291510 (erl_eval:do_apply/6 + 408)
0x00007f6958e16088:SReturn addr 0x580DF1B0 (shell:exprs/7 + 632)
0x00007f6958e160a0:SReturn addr 0x580DE8D0 (shell:eval_exprs/7 + 128)
0x00007f6958e160f8:SReturn addr 0x580DE4C8 (shell:eval_loop/3 + 576)
0x00007f6958e16130:SReturn addr 0x943E78 (<terminate process normally>)

Erlang is built from source using kerl. This used to work on Debian
8.9 (as far as I remember, but I may be wrong here) and this also
works (at least doesn't crash the VM) on Erlang 18.3:

3> inet:sockname(hd(erlang:ports())).
** exception error: no try clause matching []
     in function  prim_inet:ctl_cmd/3
     in call from prim_inet:sockname/1

Sometimes my terminal hangs when I reproduce this bug. I just checked
and I got similar crash on OS X too with Erlang/OTP 19.3.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Erlang 19.3 crashes when getting sockname of stdin

Stanislaw Klekot
On Mon, Oct 16, 2017 at 02:14:13PM +0200, Attila Rajmund Nohl wrote:
> Hello!
>
> I have a oneliner that prints out which erlang process is listening on
> a given TCP port.

So basically the same thing as inet:i/0 does. Granted, it's not a
documented function.

> Recently I upgraded from Debian 8.9 to Debian 9.2
> and on Erlang/OTP 19.3.1 this oneliner no longer works. The minimal
> example to reproduce:
>
> Erlang/OTP 19 [erts-8.3.1] [source] [64-bit] [smp:2:2]
> [async-threads:10] [hipe] [kernel-poll:false]
>
> Eshell V8.3.1  (abort with ^G)
> 1> inet:sockname(self()).
> ** exception error: no function clause matching prim_inet:sockname(<0.58.0>)
> 2> inet:sockname(hd(erlang:ports())).
> Failed to write to erl_child_setup: 9
>
> Crash dump is being written to: erl_crash.dump...done

inet:sockname() uses erlang:port_control(), which is a private command
interface. Each port driver can interpret the command as it sees fit,
without any guarantee that the result will be sane when you send
a command intended for a totally different driver.

In other words, don't do that even in local hacks unless you know inside
out the port driver you talk to.

If you need to use something that's not officially supported, use
inet:i() (shorter and bundled with the distribution) or dig out where
the TCP ports come from.

--
Stanislaw Klekot
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Erlang 19.3 crashes when getting sockname of stdin

Danil Zagoskin-2
In reply to this post by Attila Rajmund Nohl
Hi!
Before calling inet:sockname on a port first make sure it's inet_tcp or inet_udp.
For example,
[{inet:sockname(P), erlang:port_info(P, connected)} || P <- erlang:ports(), erlang:port_get_data(P) == inet_tcp].

You also can filter by name from erlang:port_info/1:
[{inet:sockname(P), erlang:port_info(P, connected)} || P <- erlang:ports(), erlang:port_info(P, name) == {name,"tcp_inet"}].


On Mon, Oct 16, 2017 at 3:14 PM, Attila Rajmund Nohl <[hidden email]> wrote:
Hello!

I have a oneliner that prints out which erlang process is listening on
a given TCP port. Recently I upgraded from Debian 8.9 to Debian 9.2
and on Erlang/OTP 19.3.1 this oneliner no longer works. The minimal
example to reproduce:

Erlang/OTP 19 [erts-8.3.1] [source] [64-bit] [smp:2:2]
[async-threads:10] [hipe] [kernel-poll:false]

Eshell V8.3.1  (abort with ^G)
1> inet:sockname(self()).
** exception error: no function clause matching prim_inet:sockname(<0.58.0>)
2> inet:sockname(hd(erlang:ports())).
Failed to write to erl_child_setup: 9

Crash dump is being written to: erl_crash.dump...done

The (relevant?) part of the crash dump:

Current Process: <0.60.0>
Current Process State: Running
Current Process Internal State: ACT_PRIO_NORMAL | USR_PRIO_NORMAL |
PRQ_PRIO_NORMAL | ACTIVE | RUNNING | ON_HEAP_MSGQ
Current Process Program counter: 0x00007f695af4b1d0 (unknown function)
Current Process CP: 0x0000000000000000 (invalid)
Current Process Limited Stack Trace:
0x00007f6958e16048:SReturn addr 0x5C473830 (prim_inet:ctl_cmd/3 + 64)
0x00007f6958e16070:SReturn addr 0x5C45F3F8 (prim_inet:sockname/1 + 96)
0x00007f6958e16080:SReturn addr 0x5C291510 (erl_eval:do_apply/6 + 408)
0x00007f6958e16088:SReturn addr 0x580DF1B0 (shell:exprs/7 + 632)
0x00007f6958e160a0:SReturn addr 0x580DE8D0 (shell:eval_exprs/7 + 128)
0x00007f6958e160f8:SReturn addr 0x580DE4C8 (shell:eval_loop/3 + 576)
0x00007f6958e16130:SReturn addr 0x943E78 (<terminate process normally>)

Erlang is built from source using kerl. This used to work on Debian
8.9 (as far as I remember, but I may be wrong here) and this also
works (at least doesn't crash the VM) on Erlang 18.3:

3> inet:sockname(hd(erlang:ports())).
** exception error: no try clause matching []
     in function  prim_inet:ctl_cmd/3
     in call from prim_inet:sockname/1

Sometimes my terminal hangs when I reproduce this bug. I just checked
and I got similar crash on OS X too with Erlang/OTP 19.3.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions



--
Danil Zagoskin | [hidden email]

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Erlang 19.3 crashes when getting sockname of stdin

Lukas Larsson-8
Hello,

On Mon, Oct 16, 2017 at 3:18 PM, Danil Zagoskin <[hidden email]> wrote:

Before calling inet:sockname on a port first make sure it's inet_tcp or inet_udp.
For example,
[{inet:sockname(P), erlang:port_info(P, connected)} || P <- erlang:ports(), erlang:port_get_data(P) == inet_tcp].

You also can filter by name from erlang:port_info/1:
[{inet:sockname(P), erlang:port_info(P, connected)} || P <- erlang:ports(), erlang:port_info(P, name) == {name,"tcp_inet"}].


Note that even this approach is not future proof as it assumes that tcp connection are ports. Something which we are planning to change in the future.

Lukas

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Erlang 19.3 crashes when getting sockname of stdin

Richard A. O'Keefe-2


On 17/10/17 4:51 AM, Lukas Larsson wrote:

> Note that even this approach is not future proof as it assumes that tcp
> connection are ports. Something which we are planning to change in the
> future.
Would you care to say more about this?

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Erlang 19.3 crashes when getting sockname of stdin

Lukas Larsson-8
On Tue, Oct 17, 2017 at 1:38 AM, Richard A. O'Keefe <[hidden email]> wrote:


On 17/10/17 4:51 AM, Lukas Larsson wrote:

Note that even this approach is not future proof as it assumes that tcp
connection are ports. Something which we are planning to change in the
future.
Would you care to say more about this?


We are planning to rewrite a lot of functionality in erts that today uses linked-in driver to use nifs instead. We have already re-written the zlib driver, we are in the process of re-writing the efile driver and are planning to rewrite the inet driver in the future.

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Erlang 19.3 crashes when getting sockname of stdin

zxq9-2
On 2017年10月17日 火曜日 13:19:53 Lukas Larsson wrote:
>
> We are planning to rewrite a lot of functionality in erts that today uses
> linked-in driver to use nifs instead. We have already re-written the zlib
> driver, we are in the process of re-writing the efile driver and are
> planning to rewrite the inet driver in the future.

What is the rationale?

-Craig
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Erlang 19.3 crashes when getting sockname of stdin

Lukas Larsson-3
On Tue, Oct 17, 2017 at 1:29 PM, zxq9 <[hidden email]> wrote:
On 2017年10月17日 火曜日 13:19:53 Lukas Larsson wrote:
>
> We are planning to rewrite a lot of functionality in erts that today uses
> linked-in driver to use nifs instead. We have already re-written the zlib
> driver, we are in the process of re-writing the efile driver and are
> planning to rewrite the inet driver in the future.

What is the rationale?

It varies depending on driver. In general we want to be able to write more code in Erlang and less in C than we are able to with linked-in drivers.

In zlib we wanted to improve how we handle large inputs, i.e. yield back to schedulers etc.
For efile we want to start using dirty schedulers for file IO, something that is not possible with linked-in drivers.
For inet the main reasons are maintainability and performance. We want to be able to add and extend functionality more easily. We also think that it should be possible to write a faster implementation using nifs because sometimes we can remove the context switch between process and port that is mandatory today.

Lukas

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions