Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential deadlock or goleak in lib/semaphore/semaphore.go#66 #9433

Closed
xuxiaofan1203 opened this issue Feb 24, 2024 · 1 comment
Closed

Potential deadlock or goleak in lib/semaphore/semaphore.go#66 #9433

xuxiaofan1203 opened this issue Feb 24, 2024 · 1 comment
Labels
bug A problem with current functionality, as opposed to missing functionality (enhancement) needs-triage New issues needed to be validated

Comments

@xuxiaofan1203
Copy link

In the case, s.Take(75) and s.SetCapacity(10) are called. At the moment, size = max = 10 and available = 0, if s.Take(75) is called again,
the goroutine will block at s.cond.Wait() even if s.Give(75) is called after that. I think the result is strange, the Wait() should block due to available = 0, but if there are enough available later, the Wait() should be signaled instead of causing a deadlock or goleak.

func TestByteSempahoreCapChangeDown2(t *testing.T) {
	// Things should make sense when capacity is adjusted down, different case
	s := NewSemaphore(100)

	s.Take(75)
	if s.available != 25 {
		t.Error("bad state after take")
	}

	s.SetCapacity(10)
	if s.available != 0 {
		t.Error("bad state after adjust")
	}
	s.Take(75)
	s.Give(75)
	if s.available != 10 {
		t.Error("bad state after give")
	}
}

图片

@xuxiaofan1203 xuxiaofan1203 added bug A problem with current functionality, as opposed to missing functionality (enhancement) needs-triage New issues needed to be validated labels Feb 24, 2024
@calmh
Copy link
Member

calmh commented Feb 24, 2024

This is a problem in your test case, you can't use the semaphore like this.

@calmh calmh closed this as not planned Won't fix, can't repro, duplicate, stale Feb 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A problem with current functionality, as opposed to missing functionality (enhancement) needs-triage New issues needed to be validated
Projects
None yet
Development

No branches or pull requests

2 participants