Skip to content

Commit

Permalink
UDP receiver async - fix panic during shutdown (async mode only) (ope…
Browse files Browse the repository at this point in the history
…n-telemetry#29121)

**Description:** 
Link to tracking Issue: Fixes open-telemetry#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.
  • Loading branch information
hovavza authored and RoryCrispin committed Nov 24, 2023
1 parent e1ab535 commit 84d75f1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
27 changes: 27 additions & 0 deletions .chloggen/pkg-stanza-input-udp-async-fix-stop-panic.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: pkg/stanza

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix panic during stop for udp async mode only.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [29120]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
14 changes: 8 additions & 6 deletions pkg/stanza/operator/input/udp/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ type Input struct {
connection net.PacketConn
cancel context.CancelFunc
wg sync.WaitGroup
wgReader sync.WaitGroup

encoding encoding.Encoding
splitFunc bufio.SplitFunc
Expand Down Expand Up @@ -200,7 +201,7 @@ func (u *Input) goHandleMessages(ctx context.Context) {
}

for i := 0; i < u.AsyncConfig.Readers; i++ {
u.wg.Add(1)
u.wgReader.Add(1)
go u.readMessagesAsync(ctx)
}

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

func (u *Input) readMessagesAsync(ctx context.Context) {
defer u.wg.Done()
defer u.wgReader.Done()

for {
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)
Expand Down Expand Up @@ -372,10 +373,6 @@ func (u *Input) removeTrailingCharactersAndNULsFromBuffer(buffer []byte, n int)
// Stop will stop listening for udp messages.
func (u *Input) Stop() error {
u.stopOnce.Do(func() {
if u.AsyncConfig != nil {
close(u.messageQueue)
}

if u.cancel == nil {
return
}
Expand All @@ -385,6 +382,11 @@ func (u *Input) Stop() error {
u.Errorf("failed to close UDP connection: %s", err)
}
}
if u.AsyncConfig != nil {
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)
close(u.messageQueue)
}

u.wg.Wait()
if u.resolver != nil {
u.resolver.Stop()
Expand Down

0 comments on commit 84d75f1

Please sign in to comment.