Skip to content

fix(backup): avoid hang when closing unbound channel - #18

Open
vividctrlalt wants to merge 1 commit into
developfrom
fix/backup_server_close_race
Open

fix(backup): avoid hang when closing unbound channel#18
vividctrlalt wants to merge 1 commit into
developfrom
fix/backup_server_close_race

Conversation

@vividctrlalt

Copy link
Copy Markdown
Owner

What

BackupServer.close() can hang if it races start()'s bind, and the keep-alive task can write() after the UDP channel is already closed (logger.error("Exception in send keep alive")).

Why

channel was a plain field next to a volatile boolean shutdown. Those two writes form a Dekker pair: close() must observe a completed bind, and start() must observe shutdown after publishing channel. Without volatile on channel, close() can see a still-null channel, skip channel.close(), and leave closeFuture().sync() blocked.

The keep-alive scheduler is a second window. If the channel is closed while the scheduled task is still eligible to run, MessageHandler.accept() writes to a closed channel. BackupManager.stop() waits for the executor and cannot be used for that first step.

Changes

  • Mark BackupServer.channel volatile (Dekker with shutdown).
  • After bind in start(), if shutdown is already set, close the channel immediately.
  • close() order: stop the keep-alive scheduler without waiting (BackupManager.stopScheduler()shutdown() only), close the channel, then backupManager.stop().
  • BackupServerTest polls isBound() instead of Thread.sleep(1000).
  • chooseRandomPort / checkPortAvailable also probe UDP. setReuseAddress after bind is removed (the socket is already bound; the call does not affect the bind that just succeeded and can report a port as free when it is not).

Test plan

  • ./gradlew :framework:test --tests org.tron.common.backup.BackupServerTest

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