NullPointerException when stopping a just-created TCP server in an application?

25 views
Skip to first unread message

Laird Nelson

unread,
Oct 27, 2011, 12:41:28 PM10/27/11
to h2-da...@googlegroups.com
I'm receiving a NPE with H2 version 1.3.160 when I call Server#stop() from within an application.

Immediately prior to that I've called Server#start().

Here is the stack:

java.lang.NullPointerException
    at org.h2.value.Transfer.close(Transfer.java:283)
    at org.h2.server.TcpServerThread.close(TcpServerThread.java:176)
    at org.h2.server.TcpServer.stop(TcpServer.java:301)
    at org.h2.tools.Server.stop(Server.java:424)
[snip]
java.lang.NullPointerException
    at org.h2.value.Transfer.init(Transfer.java:78)
    at org.h2.server.TcpServerThread.run(TcpServerThread.java:63)
    at java.lang.Thread.run(Thread.java:680)

My calling code looks like this:

server.start();
server.stop();

I didn't see anywhere in the H2 documentation where to report bugs (the search string "bugs" returned 0 results :-)), so I'm reporting it here.

Thanks for a great product.

Best,
Laird

Thomas Mueller

unread,
Oct 27, 2011, 1:54:38 PM10/27/11
to h2-da...@googlegroups.com
Hi,

I can't reproduce the problem. Could you post a complete, simple, reproducible test case? See also http://sscce.org/

Regards,
Thomas

Laird Nelson

unread,
Oct 27, 2011, 2:55:31 PM10/27/11
to h2-da...@googlegroups.com
That's interesting!  Yes, of course I will--I only didn't because of just how simple my code is.

Coming up shortly.

Best,
Laird

Laird Nelson

unread,
Oct 27, 2011, 3:21:08 PM10/27/11
to h2-da...@googlegroups.com
Hi, Thomas; this code below reproduces the problem faithfully on my MacBook Pro using Java 6:

Am I doing anything obviously wrong below?

package ljnelson.h2npe;

import org.h2.tools.Server;

import org.junit.Test;

import static org.junit.Assert.assertNotNull;

public class TestCaseNullPointerExceptionOnStop {

  @Test
  public void testNullPointerExceptionOnStop() throws Exception {
    final Server server = Server.createTcpServer("-basedir", System.getProperty("user.home"), "-trace", "-tcp", "-tcpPort", "9092");
    assertNotNull(server);
    server.start();
    server.stop(); // kaboom
  }
 
}

I will wrap it up and attach it shortly.

Best,
Laird

Laird Nelson

unread,
Oct 27, 2011, 3:22:52 PM10/27/11
to h2-da...@googlegroups.com
Hmm; one thing I'm noticing is that this NPE is not preventing the stop() operation from completing.  So perhaps this is log/trace output?

L

Laird Nelson

unread,
Oct 27, 2011, 3:27:55 PM10/27/11
to h2-da...@googlegroups.com
Test case attached; unzip it somewhere, cd h2npe-1.0-SNAPSHOT, then using Maven run:

mvn clean test

...to reproduce.

Best,
Laird
h2-npe-1.0-SNAPSHOT-src.zip

Laird Nelson

unread,
Oct 27, 2011, 3:31:00 PM10/27/11
to h2-da...@googlegroups.com
Confirmed that without the "-trace" argument during server creation this output goes away.  I suspect the root problem is still in the codepath however.

Best,
Laird

Noel Grandin

unread,
Oct 28, 2011, 4:24:48 AM10/28/11
to h2-da...@googlegroups.com, Thomas Mueller
Transfer#close() is being called before Transfer#init() has been called.
close() just needs to check if out != null before calling out.flush().

To be honest, this is a weird test-case. People don't normally call stop() immediately after start().

But the whole construction of Transfer is a little dodgy. I would prefer to initialise the fields [socket,in,out,ssl] in
the constructor and make them final, so that we know for sure that they are initialised before the constructor returns.

I can't see any point in the current process because we always init() immediately after calling the constructor, except
for one place in TcpServerThread where we might as well call it immediately because we already have a functioning socket.

That way close() won't need any changes, and the code becomes safer.

-- Noel

> --
> You received this message because you are subscribed to the Google Groups "H2 Database" group.
> To post to this group, send email to h2-da...@googlegroups.com.
> To unsubscribe from this group, send email to h2-database...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/h2-database?hl=en.

Thomas Mueller

unread,
Oct 28, 2011, 4:36:19 AM10/28/11
to h2-da...@googlegroups.com
Hi,

Thanks for the test case! I can reproduce it now.

Those exceptions are expected, as the server socket is closed while a
connection is still open. The code is a bit strange, but it's nothing
serious. However, I see that it doesn't "look nice", and I will change
the code so that such exceptions are simply ignored, and not logged,
even when using the "-trace" option.

Regards,
Thomas

Laird Nelson

unread,
Oct 28, 2011, 11:05:36 AM10/28/11
to h2-da...@googlegroups.com, Thomas Mueller
On Friday, October 28, 2011 4:24:48 AM UTC-4, Noel Grandin wrote:
To be honest, this is a weird test-case. People don't normally call stop() immediately after start().

Absolutely correct.  I am writing a Maven plugin for H2, and one of the things I want it to do is start a TCP server.  Part of my unit test was simply to start the server, then stop it, and verify that nothing is running.

Best,
Laird

--
http://about.me/lairdnelson
Reply all
Reply to author
Forward
0 new messages