Commit b7902e2
sidecar: fix resource leak and state corruption in UdpWriter
Address additional bugs in the reconnect logic beyond the initial
thread-safety fix:
1. **Resource leak**: If DatagramChannel.open() succeeds but connect()
fails, the opened channel is never closed, causing file descriptor
exhaustion.
2. **State corruption**: If connect() fails after opening the channel,
this.channel points to an open but unconnected channel. Subsequent
writes throw NotYetConnectedException (not caught by the
ClosedChannelException handler), permanently breaking the writer.
3. **Data loss**: Original code dropped data after reconnection even
if reconnection succeeded.
**Changes:**
- Use local variable in connect() and only assign on success
- Close channel in connect() if connection fails (prevents leak)
- Retry write after successful reconnection (prevents data loss)
- Handle case where another thread already reconnected
- Update test to verify data delivery after reconnection
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 4.5 <[email protected]>1 parent 238c775 commit b7902e2
File tree
2 files changed
+29
-4
lines changed- spectator-reg-sidecar/src
- main/java/com/netflix/spectator/sidecar
- test/java/com/netflix/spectator/sidecar
2 files changed
+29
-4
lines changedLines changed: 26 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | | - | |
48 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
49 | 58 | | |
50 | 59 | | |
51 | 60 | | |
| |||
60 | 69 | | |
61 | 70 | | |
62 | 71 | | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
63 | 76 | | |
64 | 77 | | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
65 | 89 | | |
66 | 90 | | |
67 | 91 | | |
68 | | - | |
69 | 92 | | |
70 | 93 | | |
71 | 94 | | |
| |||
Lines changed: 3 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | | - | |
| 47 | + | |
48 | 48 | | |
49 | 49 | | |
| 50 | + | |
| 51 | + | |
50 | 52 | | |
51 | 53 | | |
52 | 54 | | |
| |||
0 commit comments