Skip to content

Conversation

@dwickern
Copy link
Contributor

Fixes #6864

NetworkChannel starts two threads which may reference outputBuffer before the field is initialized, resulting in a NPE:

Exception in thread "sbt-networkchannel-0" java.lang.NullPointerException: Cannot enter synchronized block because "this.sbt$internal$server$NetworkChannel$$outputBuffer" is null
        at sbt.internal.server.NetworkChannel.sbt$internal$server$NetworkChannel$$doFlush(NetworkChannel.scala:672)
        at sbt.internal.server.NetworkChannel.shutdown(NetworkChannel.scala:568)
        at sbt.internal.server.NetworkChannel.shutdown(NetworkChannel.scala:554)
        at sbt.internal.server.NetworkChannel$$anon$3.run(NetworkChannel.scala:190)

It's easier to see in the disassembly:

public NetworkChannel(...) {
    ...
    this.thread = new Thread(...); // calls shutdown
    this.thread().start();
    this.writeThread = new Thread(...); // also calls shutdown
    this.writeThread.setDaemon(true);
    this.writeThread.start();
    this.sbt$internal$server$NetworkChannel$$outputBuffer = new LinkedBlockingQueue();
    this.sbt$internal$server$NetworkChannel$$flushExecutor = Executors.newSingleThreadScheduledExecutor(...);
}

This PR moves the thread start to the end of the constructor.

Copy link
Member

@eed3si9n eed3si9n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@eed3si9n eed3si9n merged commit d99163b into sbt:1.10.x Jan 12, 2025
10 checks passed
@dwickern dwickern deleted the fix-npe branch January 12, 2025 05:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"sbt-networkchannel-0" java.lang.NullPointerException

2 participants