Skip to content

Commit c3d4d65

Browse files
authored
UDP receiver async - fix panic during shutdown (async mode only) (#29121)
**Description:** Link to tracking Issue: Fixes #29120 Fixing a bug - panic happens during stop method in async mode only (didn't affect the default non-async mode). When stop is called, it closes the messageQueue channel, signaling to processMessagesAsync to stop running. However, readMessagesAsync sometimes tries to write into the closed channel (depends whether the method is currently reading from the closed connection or currently trying to write to the channel), and as a result, panic error happens. Separated between wg (waitGroup that serves non-async code and processMessagesAsync) & the new wg_reader (waitGroup serving readMessagesAsync only). This allows us to first stop readMessagesAsync, wait for it to finish, before closing the channel. Instead, stop (in async mode) should do the following: 1. Close the connection - signaling readMessagesAsync to stop - the messageQueue channel will remain open until that method is done so there's no risk of panic (due to writing to a closed channel). 2. Wait for readMessagesAsync to finish (wait for new wg_reader). 3. Close messageQueue channel (signaling processMessagesAsync to stop) 4. Wait for processMessagesAsync to finish (wait sg). **Link to tracking Issue:** 29120 **Testing:** Unitests ran. Ran concrete strato, stopped & restarted multiple times, didn't see any panic (and stop completed successfully as expected) **Documentation:** None.
1 parent 794e127 commit c3d4d65

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: pkg/stanza
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Fix panic during stop for udp async mode only.
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [29120]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

pkg/stanza/operator/input/udp/udp.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ type Input struct {
160160
connection net.PacketConn
161161
cancel context.CancelFunc
162162
wg sync.WaitGroup
163+
wgReader sync.WaitGroup
163164

164165
encoding encoding.Encoding
165166
splitFunc bufio.SplitFunc
@@ -200,7 +201,7 @@ func (u *Input) goHandleMessages(ctx context.Context) {
200201
}
201202

202203
for i := 0; i < u.AsyncConfig.Readers; i++ {
203-
u.wg.Add(1)
204+
u.wgReader.Add(1)
204205
go u.readMessagesAsync(ctx)
205206
}
206207

@@ -255,7 +256,7 @@ func (u *Input) processMessage(ctx context.Context, message []byte, remoteAddr n
255256
}
256257

257258
func (u *Input) readMessagesAsync(ctx context.Context) {
258-
defer u.wg.Done()
259+
defer u.wgReader.Done()
259260

260261
for {
261262
readBuffer := u.readBufferPool.Get().(*[]byte) // Can't reuse the same buffer since same references would be written multiple times to the messageQueue (and cause data override of previous entries)
@@ -372,10 +373,6 @@ func (u *Input) removeTrailingCharactersAndNULsFromBuffer(buffer []byte, n int)
372373
// Stop will stop listening for udp messages.
373374
func (u *Input) Stop() error {
374375
u.stopOnce.Do(func() {
375-
if u.AsyncConfig != nil {
376-
close(u.messageQueue)
377-
}
378-
379376
if u.cancel == nil {
380377
return
381378
}
@@ -385,6 +382,11 @@ func (u *Input) Stop() error {
385382
u.Errorf("failed to close UDP connection: %s", err)
386383
}
387384
}
385+
if u.AsyncConfig != nil {
386+
u.wgReader.Wait() // only when all async readers are finished, so there's no risk of sending to a closed channel, do we close messageQueue (which allows the async processors to finish)
387+
close(u.messageQueue)
388+
}
389+
388390
u.wg.Wait()
389391
if u.resolver != nil {
390392
u.resolver.Stop()

0 commit comments

Comments
 (0)