Skip to content

Conversation

@matthewperiut
Copy link
Contributor

With threaded networking, custom payload packets can be received and handled on the network thread, where Minecraft.getNetworkHandler() may return null due to a race condition the handler that delivered the packet may not yet be (or may no longer be) assigned on the Minecraft instance. The previous fix of deferring to the main thread via ensureOnMainThread() didn't fully solve this either, since the handler could still be nulled out between ticks. This fix instead captures the network handler at packet arrival time where it is guaranteed to be non-null and stores it in the Context object and a ThreadLocal so that it is available when sending response packets from within a listener callback. A ThreadLocal is used rather than threading the handler through as a method parameter because sendPacket() is reached through the public send/sendNoCheck API, and changing those signatures would break the end-user API.

I reverted the now unnecessary ensureOnMainThread with this commit/pr as well, which only partially resolved the issue.

I investigated other places where null network handler may be an issue, but the other uses of minecraft.getNetworkHandler() are covered by null checks in isPlayReady, so it's good to go.

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.

1 participant