Weijun Wang | 13 Jun 2012 10:38
Picon
Favicon

GC closes my ServerSocket

Hi All

I have a test that basically looks like:

     int p = new ServerSocket(0).getLocalPort();
     //....
     new Socket("localhost", p);

Recently it's failing on solaris-i586, and after some investigation, I 
realize that the ServerSocket object is GC'ed and auto-closed.

(But why only recently?)

So I change the first line to

     ServerSocket ss = new ServerSocket(0);
     int p = ss.getLocalPort();

and it's running fine.

I want to know if the ServerSocket object still has a chance to be 
closed. If yes, I'll add a

     ss.close();

at the end to be safer.

Thanks
Max

(Continue reading)

Alan Bateman | 13 Jun 2012 10:51
Picon
Favicon

Re: GC closes my ServerSocket

On 13/06/2012 09:38, Weijun Wang wrote:
> Hi All
>
> I have a test that basically looks like:
>
>     int p = new ServerSocket(0).getLocalPort();
>     //....
>     new Socket("localhost", p);
>
> Recently it's failing on solaris-i586, and after some investigation, I 
> realize that the ServerSocket object is GC'ed and auto-closed.
>
> (But why only recently?)
>
> So I change the first line to
>
>     ServerSocket ss = new ServerSocket(0);
>     int p = ss.getLocalPort();
>
> and it's running fine.
>
> I want to know if the ServerSocket object still has a chance to be 
> closed. If yes, I'll add a
>
>     ss.close();
>
> at the end to be safer.
>
> Thanks
> Max
(Continue reading)

Chris Hegarty | 13 Jun 2012 11:08
Picon
Favicon

Re: GC closes my ServerSocket


On 13/06/2012 09:51, Alan Bateman wrote:
> On 13/06/2012 09:38, Weijun Wang wrote:
>> Hi All
>>
>> I have a test that basically looks like:
>>
>> int p = new ServerSocket(0).getLocalPort();
>> //....
>> new Socket("localhost", p);
>>
>> Recently it's failing on solaris-i586, and after some investigation, I
>> realize that the ServerSocket object is GC'ed and auto-closed.
>>
>> (But why only recently?)
>>
>> So I change the first line to
>>
>> ServerSocket ss = new ServerSocket(0);
>> int p = ss.getLocalPort();
>>
>> and it's running fine.
>>
>> I want to know if the ServerSocket object still has a chance to be
>> closed. If yes, I'll add a
>>
>> ss.close();
>>
>> at the end to be safer.
>>
(Continue reading)

Weijun Wang | 13 Jun 2012 11:23
Picon
Favicon

Re: GC closes my ServerSocket

Please anyone take a review:

    http://cr.openjdk.java.net/~weijun/7176574/webrev.00/

By assigning to a local variable hopefully it stays alive on the stack 
during the whole method.

Noreg-self.

*Chris*: I didn't indented the whole test by wrapping them into a 
try-finally (or try-with-resources) block. The test runs in othervm and 
I guess the sockets will be closed anyway.

Thanks
Max

On 06/13/2012 05:08 PM, Chris Hegarty wrote:
>
>
> On 13/06/2012 09:51, Alan Bateman wrote:
>> On 13/06/2012 09:38, Weijun Wang wrote:
>>> Hi All
>>>
>>> I have a test that basically looks like:
>>>
>>> int p = new ServerSocket(0).getLocalPort();
>>> //....
>>> new Socket("localhost", p);
>>>
>>> Recently it's failing on solaris-i586, and after some investigation, I
(Continue reading)

Chris Hegarty | 13 Jun 2012 11:52
Picon
Favicon

Re: GC closes my ServerSocket


On 13/06/2012 10:23, Weijun Wang wrote:
> Please anyone take a review:
>
> http://cr.openjdk.java.net/~weijun/7176574/webrev.00/
>
> By assigning to a local variable hopefully it stays alive on the stack
> during the whole method.

If they don't then we have a real problem ;-)

> Noreg-self.
>
> *Chris*: I didn't indented the whole test by wrapping them into a
> try-finally (or try-with-resources) block. The test runs in othervm and
> I guess the sockets will be closed anyway.

Strictly speaking, after the construction of these ServerSocket instance 
you should wrap the remainder of the code in a try finally to ensure 
that close is called. Even with othervm mode when the vm is exiting 
there is not guarantee that finalizers are run.

I think it would be best to add a finally block here to reduce the 
chances of strange/intermittent behavior as a result of running this 
test. Either the test itself or excessive resource hogging on the 
system. But if you are really against it, I can live with what you have 
;-)

-Chris.

(Continue reading)

Weijun Wang | 13 Jun 2012 12:50
Picon
Favicon

Re: GC closes my ServerSocket

Updated:

   http://cr.openjdk.java.net/~weijun/7176574/webrev.01/

Thanks
Max

On 06/13/2012 05:52 PM, Chris Hegarty wrote:
>
>
> On 13/06/2012 10:23, Weijun Wang wrote:
>> Please anyone take a review:
>>
>> http://cr.openjdk.java.net/~weijun/7176574/webrev.00/
>>
>> By assigning to a local variable hopefully it stays alive on the stack
>> during the whole method.
>
> If they don't then we have a real problem ;-)
>
>> Noreg-self.
>>
>> *Chris*: I didn't indented the whole test by wrapping them into a
>> try-finally (or try-with-resources) block. The test runs in othervm and
>> I guess the sockets will be closed anyway.
>
> Strictly speaking, after the construction of these ServerSocket instance
> you should wrap the remainder of the code in a try finally to ensure
> that close is called. Even with othervm mode when the vm is exiting
> there is not guarantee that finalizers are run.
(Continue reading)

Chris Hegarty | 13 Jun 2012 13:04
Picon
Favicon

Re: GC closes my ServerSocket

Looks fine to me. Thanks for adding the finally block.

-Chris.

On 13/06/2012 11:50, Weijun Wang wrote:
> Updated:
>
> http://cr.openjdk.java.net/~weijun/7176574/webrev.01/
>
> Thanks
> Max
>
> On 06/13/2012 05:52 PM, Chris Hegarty wrote:
>>
>>
>> On 13/06/2012 10:23, Weijun Wang wrote:
>>> Please anyone take a review:
>>>
>>> http://cr.openjdk.java.net/~weijun/7176574/webrev.00/
>>>
>>> By assigning to a local variable hopefully it stays alive on the stack
>>> during the whole method.
>>
>> If they don't then we have a real problem ;-)
>>
>>> Noreg-self.
>>>
>>> *Chris*: I didn't indented the whole test by wrapping them into a
>>> try-finally (or try-with-resources) block. The test runs in othervm and
>>> I guess the sockets will be closed anyway.
(Continue reading)

Neil Richards | 13 Jun 2012 14:23

Re: GC closes my ServerSocket

On Wed, 2012-06-13 at 17:23 +0800, Weijun Wang wrote:
> Please anyone take a review:
> 
>     http://cr.openjdk.java.net/~weijun/7176574/webrev.00/
> 
> By assigning to a local variable hopefully it stays alive on the stack 
> during the whole method.
> 
> Noreg-self.
> 
> *Chris*: I didn't indented the whole test by wrapping them into a 
> try-finally (or try-with-resources) block. The test runs in othervm and 
> I guess the sockets will be closed anyway.
> 
> Thanks
> Max
> 
> On 06/13/2012 05:08 PM, Chris Hegarty wrote:
> >
> >
> > On 13/06/2012 09:51, Alan Bateman wrote:
> >> On 13/06/2012 09:38, Weijun Wang wrote:
> >>> Hi All
> >>>
> >>> I have a test that basically looks like:
> >>>
> >>> int p = new ServerSocket(0).getLocalPort();
> >>> //....
> >>> new Socket("localhost", p);
> >>>
(Continue reading)

Weijun Wang | 13 Jun 2012 14:48
Picon
Favicon

Re: GC closes my ServerSocket

 > Would this be a good candidate to use Automatic Resource Management ?

Correct. I'll remember to use it the next time.

Thanks
Max

On 06/13/2012 08:23 PM, Neil Richards wrote:
> On Wed, 2012-06-13 at 17:23 +0800, Weijun Wang wrote:
>> Please anyone take a review:
>>
>>      http://cr.openjdk.java.net/~weijun/7176574/webrev.00/
>>
>> By assigning to a local variable hopefully it stays alive on the stack
>> during the whole method.
>>
>> Noreg-self.
>>
>> *Chris*: I didn't indented the whole test by wrapping them into a
>> try-finally (or try-with-resources) block. The test runs in othervm and
>> I guess the sockets will be closed anyway.
>>
>> Thanks
>> Max
>>
>> On 06/13/2012 05:08 PM, Chris Hegarty wrote:
>>>
>>>
>>> On 13/06/2012 09:51, Alan Bateman wrote:
>>>> On 13/06/2012 09:38, Weijun Wang wrote:
(Continue reading)

Jim Gish | 13 Jun 2012 15:57
Picon
Favicon

Re: GC closes my ServerSocket



On 06/13/2012 08:48 AM, Weijun Wang wrote:
Automatic Resource Management ?

http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

try-with-resources

I learned about it when I submitted a test change using try finally (It was recommend that I use this instead :-) )

Jim

Gmane