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

Pipe with SharedBuffer channels will be dead lock #28

Open
homerzhou opened this issue Feb 20, 2024 · 2 comments
Open

Pipe with SharedBuffer channels will be dead lock #28

homerzhou opened this issue Feb 20, 2024 · 2 comments
Labels

Comments

@homerzhou
Copy link

homerzhou commented Feb 20, 2024

Fllow is the example code, the example code is from eapache/channels readme.

// never more than 3 elements in the pipeline at once
buf := NewSharedBuffer(3)

ch1 := buf.NewChannel()
ch2 := buf.NewChannel()

// or, instead of a straight pipe, implement your pipeline step
Pipe(ch1, ch2)

// inputs
go func() {
	for i := 0; i < 20; i++ {
		ch1.In() <- i
	}
	ch1.Close()
}()

time.Sleep(10 * time.Second)

for _ = range ch2.Out() {
	// outputs
}

buf.Close()

the flow graph is blow
data => ch1-in => ch1-out => ch2-in => ch2-out => data

Fllow scenario will deadlock

  1. ch1's length is 3, ch2's length is 0, all recv blocked
  2. ch1-out read one data, before send to ch2-in
  3. ch1's length is 2, ch2's length is 0, all recv unblocked
  4. ch1-in have another data in
  5. ch1's length is 3, ch2's length is 0, all recv blocked
  6. than the second data can not insert into ch2-in, event ch2-out is always ready
@eapache eapache added the bug label Feb 20, 2024
@eapache
Copy link
Owner

eapache commented Feb 20, 2024

Yeah, this is a good discovery. I don’t know how to fix it off the top of my head, in hindsight the structure of a lot of the code in this package isn’t very reliable.

@eapache
Copy link
Owner

eapache commented Feb 20, 2024

Perhaps we could reserve one buffer slot for each created channel (leaving the rest of the buffer slots truly shared)… this would be less efficient, and not really what you would expect from a “shared buffer”, but would avoid the deadlock. Although if somebody created a “shared buffer” of 3 and then created 10 channels off of it, I don’t know what we’d do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants