Two listener sockets on same port

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

Two listener sockets on same port

Denis Kudriashov
Hi,

I remember in past there was some reports about it. And now I was a victim of this problem which is very difficult to debug.

Pharo allows to start two listener sockets on same port. You will not see any error. State of both sockets will be identical. "socket isValid, socket status" will be always correct for both instances.
You can play with sockets with following code: 
(socket  := Socket newTCP)
setOption: 'TCP_NODELAY' value: 1;
setOption: 'SO_SNDBUF' value: 4096;
setOption: 'SO_RCVBUF' value: 4096.
socket listenOn: 40424 backlogSize: 32.

When many listener sockets are created. Only first will receive connections. So first server will work. But others will not with no way to know why.

But more dramatical scenario is when first socket becomes collected as garbage which will destroy it.
In this case You will be completely confusing why your single instance in image is not working. 
Such case is of course some bug in how you manage sockets. But that's the problem: 
It is now impossible to debug such cases.

Can we fix it? Is it possible to signal error when socket is not really listen?

Best regards,
Denis
Reply | Threaded
Open this post in threaded view
|

Re: Two listener sockets on same port

Sven Van Caekenberghe-2
Well, you should do your utmost best to prevent the situation in the first place (use startup/shutdown behaviour correctly).

I remember I had those problems a lot in the beginning of Zn, especially when running tests that sometimes failed.

Have you seen Zn's #isListening ? I think that was written to catch the case you described, but I am not sure it is 100% rock solid.

Have you seen the other methods of queries protocol of Socket ?

> On 30 Jan 2017, at 18:23, Denis Kudriashov <[hidden email]> wrote:
>
> Hi,
>
> I remember in past there was some reports about it. And now I was a victim of this problem which is very difficult to debug.
>
> Pharo allows to start two listener sockets on same port. You will not see any error. State of both sockets will be identical. "socket isValid, socket status" will be always correct for both instances.
> You can play with sockets with following code:
> (socket  := Socket newTCP)
> setOption: 'TCP_NODELAY' value: 1;
> setOption: 'SO_SNDBUF' value: 4096;
> setOption: 'SO_RCVBUF' value: 4096.
> socket listenOn: 40424 backlogSize: 32.
>
> When many listener sockets are created. Only first will receive connections. So first server will work. But others will not with no way to know why.
>
> But more dramatical scenario is when first socket becomes collected as garbage which will destroy it.
> In this case You will be completely confusing why your single instance in image is not working.
> Such case is of course some bug in how you manage sockets. But that's the problem:
> It is now impossible to debug such cases.
>
> Can we fix it? Is it possible to signal error when socket is not really listen?
>
> Best regards,
> Denis


Reply | Threaded
Open this post in threaded view
|

Re: Two listener sockets on same port

Denis Kudriashov

2017-01-30 20:25 GMT+01:00 Sven Van Caekenberghe <[hidden email]>:
Well, you should do your utmost best to prevent the situation in the first place (use startup/shutdown behaviour correctly).

I remember I had those problems a lot in the beginning of Zn, especially when running tests that sometimes failed.

Have you seen Zn's #isListening ? I think that was written to catch the case you described, but I am not sure it is 100% rock solid.

Yes. This is exactly how I fix my case.
But this post is about general problem which I think is very critical.

In #isListening code I see extra condition  "self serverSocket localPort = self port". Could you explain what case it is covered?  

Reply | Threaded
Open this post in threaded view
|

Re: Two listener sockets on same port

Sven Van Caekenberghe-2

> On 30 Jan 2017, at 21:13, Denis Kudriashov <[hidden email]> wrote:
>
>
> 2017-01-30 20:25 GMT+01:00 Sven Van Caekenberghe <[hidden email]>:
> Well, you should do your utmost best to prevent the situation in the first place (use startup/shutdown behaviour correctly).
>
> I remember I had those problems a lot in the beginning of Zn, especially when running tests that sometimes failed.
>
> Have you seen Zn's #isListening ? I think that was written to catch the case you described, but I am not sure it is 100% rock solid.
>
> Yes. This is exactly how I fix my case.
> But this post is about general problem which I think is very critical.
>
> In #isListening code I see extra condition  "self serverSocket localPort = self port". Could you explain what case it is covered?  

IIRC, the idea was that if you bind/listen unsuccessfully, that condition is not true, but again, it was a long time ago, and maybe is not bullet proof.


Reply | Threaded
Open this post in threaded view
|

Re: Two listener sockets on same port

Denis Kudriashov

2017-01-30 21:30 GMT+01:00 Sven Van Caekenberghe <[hidden email]>:
>
> In #isListening code I see extra condition  "self serverSocket localPort = self port". Could you explain what case it is covered?

IIRC, the idea was that if you bind/listen unsuccessfully, that condition is not true, but again, it was a long time ago, and maybe is not bullet proof.

Yes, I check. If port is busy then localPort will be different 
Reply | Threaded
Open this post in threaded view
|

Re: Two listener sockets on same port

Denis Kudriashov

2017-01-30 22:19 GMT+01:00 Denis Kudriashov <[hidden email]>:
Yes, I check. If port is busy then localPort will be different 

And I cover it in TCPServer with test
Reply | Threaded
Open this post in threaded view
|

Re: Two listener sockets on same port

Henrik Sperre Johansen
In reply to this post by Denis Kudriashov

On 30 Jan 2017, at 21:13 , Denis Kudriashov <[hidden email]> wrote:


2017-01-30 20:25 GMT+01:00 Sven Van Caekenberghe <[hidden email]>:
Well, you should do your utmost best to prevent the situation in the first place (use startup/shutdown behaviour correctly).

I remember I had those problems a lot in the beginning of Zn, especially when running tests that sometimes failed.

Have you seen Zn's #isListening ? I think that was written to catch the case you described, but I am not sure it is 100% rock solid.

Yes. This is exactly how I fix my case.
But this post is about general problem which I think is very critical.

In #isListening code I see extra condition  "self serverSocket localPort = self port". Could you explain what case it is covered?  


Doesn't work on Windows;

IIRC, behaviour is different on all three platforms:
- Linux raises error
- Mac assigns different port than requested
- Win assigns requested port, but receives no traffic.

How to detect the last one from image is a very good question (tm) indeed... 

Cheers,
Henry
Reply | Threaded
Open this post in threaded view
|

Re: Two listener sockets on same port

Sven Van Caekenberghe-2

> On 31 Jan 2017, at 11:56, Henrik Johansen <[hidden email]> wrote:
>
>
>> On 30 Jan 2017, at 21:13 , Denis Kudriashov <[hidden email]> wrote:
>>
>>
>> 2017-01-30 20:25 GMT+01:00 Sven Van Caekenberghe <[hidden email]>:
>> Well, you should do your utmost best to prevent the situation in the first place (use startup/shutdown behaviour correctly).
>>
>> I remember I had those problems a lot in the beginning of Zn, especially when running tests that sometimes failed.
>>
>> Have you seen Zn's #isListening ? I think that was written to catch the case you described, but I am not sure it is 100% rock solid.
>>
>> Yes. This is exactly how I fix my case.
>> But this post is about general problem which I think is very critical.
>>
>> In #isListening code I see extra condition  "self serverSocket localPort = self port". Could you explain what case it is covered?  
>>
>
> Doesn't work on Windows;
> https://pharo.fogbugz.com/f/cases/16939/Intermittent-monkey-failures-due-to-port-usage-in-server-tests
>
> IIRC, behaviour is different on all three platforms:
> - Linux raises error
> - Mac assigns different port than requested
> - Win assigns requested port, but receives no traffic.

This is the curse of being x-platform, we don't see them often (luckily) but there are important (semantic) differences that sometimes make life (very) difficult.

> How to detect the last one from image is a very good question (tm) indeed...
>
> Cheers,
> Henry


Reply | Threaded
Open this post in threaded view
|

Re: Two listener sockets on same port

Denis Kudriashov
In reply to this post by Henrik Sperre Johansen

2017-01-31 11:56 GMT+01:00 Henrik Johansen <[hidden email]>:
IIRC, behaviour is different on all three platforms:
- Linux raises error

I got same behaviour on Debian as on Mac. It assigns different port