GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [Bug][fn] Go functions do not reset idleTimer correctly. · Issue #20449 · apache/pulsar

[Bug][fn] Go functions do not reset idleTimer correctly. #20449

New issue
Copy link
New issue
Copy link
Closed
#20450
flowchartsman/pulsar
#5
flowchartsman/pulsar#5
Closed
[Bug][fn] Go functions do not reset idleTimer correctly.#20449
#20450
flowchartsman/pulsar
#5
Copy link
Labels
type/bugThe PR fixed a bug or issue reported a bug

Description

@flowchartsman
flowchartsman
opened on May 31, 2023
Issue body actions

Search before asking

  • I searched in the issues and found nothing similar.

Version

The code in instance.go which handles incoming messages and idle timeout basically boils down to this:

	idleDuration := getIdleTimeout(time.Millisecond * time.Duration(gi.context.instanceConf.killAfterIdleMs))
	idleTimer := time.NewTimer(idleDuration)
	defer idleTimer.Stop()

CLOSE:
	for {
		idleTimer.Reset(idleDuration)
		select {
		case cm := <-channel:
			// handle message
		case <-idleTimer.C:
			close(channel) // this is not great either, but that's a separate bug ;)
			break CLOSE
		}
	}

However, this does not account for the idle timer firing during work. If it does, then the reset will not have the desired effect, and the idle clause will fire erroneously on a subsequent loop, since there's now a timestamp on the channel.

Minimal reproduce step

Here's a simple repro:

package main

import "fmt"
import "time"

func main() {
	const idleDuration = 250*time.Millisecond
	work := make(chan int,2)
	work <- 1
	work <- 2
	t := time.NewTimer(idleDuration)
	EVENTLOOP:
	for {
		t.Reset(idleDuration)
		fmt.Println("waiting for new work")
		select {
		case w := <- work:
			fmt.Printf("doing work on %d\n", w)
			time.Sleep(500*time.Millisecond)
			fmt.Println("done with work")
		case <- t.C:
			fmt.Println("timed out")
			break EVENTLOOP
		}
	}
}

Run that a few times, and you'll see:

waiting for new work
doing work on 1
done with work
waiting for new work
timed out

This is exactly what would happen in a Go function, only it would then call log.Fatal

What did you expect to see?

n/a

What did you see instead?

n/a

Anything else?

The correct fix is to call Stop() on the timer and, if it returns false, drain the channel before resetting:

CLOSE:
	for {
		select {
		case cm := <-channel:
			// handle message
		case <-idleTimer.C:
			close(channel)
			break CLOSE
		}
		if !idleTimer.Stop() {
			<-idleTimer.C
		}
	}

Are you willing to submit a PR?

  • I'm willing to submit a PR!

Activity

  1. flowchartsman
    added
    type/bugThe PR fixed a bug or issue reported a bug
    on May 31, 2023
  2. flowchartsman
    added a commit that references this issue on May 31, 2023

    Rest idle timer and drain if appt during main loop

    This issue will close once commit f797ba3 is merged into the 'master' branch.
    f797ba3
  3. lhotari commented on Jun 1, 2023

    @lhotari
    lhotari
    on Jun 1, 2023
    Member
    More actions

    @flowchartsman Thanks for reporting and contributing the fix for this issue.

    I recently happened to review some code in the Pulsar Go client and it contains similar code for handling the send timeout on the producer. This is the code: https://github.com/apache/pulsar-client-go/blob/2eb340511dbf0602f7c561a1d86e179098f4662b/pulsar/producer_partition.go#L910-L924
    Does this have a similar issue?

  4. flowchartsman commented on Jun 1, 2023

    @flowchartsman
    flowchartsman
    on Jun 1, 2023
    ContributorAuthor
    More actions

    @flowchartsman Thanks for reporting and contributing the fix for this issue.

    I recently happened to review some code in the Pulsar Go client and it contains similar code for handling the send timeout on the producer. This is the code: https://github.com/apache/pulsar-client-go/blob/2eb340511dbf0602f7c561a1d86e179098f4662b/pulsar/producer_partition.go#L910-L924 Does this have a similar issue?

    From a quick glance, I'd say this looks fine. The loop is a range over the channel, which guarantees that it will be read before Reset is called, and each use of Reset is immediately followed by a control flow statement that begins the next loop iteration, so none of the timeout is expended before waiting on it again. The timer is local to the function and doesn't appear to be passed anywhere. Finally, on the off chance that the timer fires after the loop is exited, the deferred Stop should ensure that the timer is no longer active, and since C is buffered, it should be GCed, whether or not there's a pending message.

    This avoids the main problem we had here where the timer wasn't guaranteed to be read in each loop, meaning it could fire during work and be left until the next iteration.

    Reacted by Lari Hotari
  5. michaeljmarshall
    closed this as completedin #20450on Jun 1, 2023
  6. michaeljmarshall
    added 5 commits that reference this issue on Jun 1, 2023

    [fix][fn]Reset idle timer correctly (#20450)

    This issue will close once commit e05b890 is merged into the 'master' branch.
    e05b890

    [fix][fn]Reset idle timer correctly (#20450)

    This issue will close once commit c776e89 is merged into the 'master' branch.
    c776e89

    [fix][fn]Reset idle timer correctly (#20450)

    This issue will close once commit 47c55a1 is merged into the 'master' branch.
    47c55a1

    [fix][fn]Reset idle timer correctly (#20450)

    This issue will close once commit dfb2d5e is merged into the 'master' branch.
    dfb2d5e

    [fix][fn]Reset idle timer correctly (apache#20450)

    This issue will close once commit 479ddfe is merged into the '4.0_ds' branch.
    479ddfe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/bugThe PR fixed a bug or issue reported a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.