Skip to content

Multiple closes on the same result channle in MemoryQueueFactory #150

Description

@wyyolo

Why put "defer close (result)" inside the goroutine? This may lead to multiple goroutines attempting to close the result, causing panic. https://github.com/FunctionStream/function-stream/blob/main/fs/contube/memory.go#L80
Perhaps it can be managed through "sync. WaitGroup", for example:

func (f *MemoryQueueFactory) NewSourceTube(ctx context.Context, configMap ConfigMap) (<-chan Record, error) {  
	config := NewSourceQueueConfig(configMap)  
	result := make(chan Record)  
	var wg sync.WaitGroup  
	for _, topic := range config.Topics {  
		wg.Add(1)   
		t := topic  
		go func() {  
			<-ctx.Done()      
			f.release(t)      
		}()  
  
		go func() {  
			defer wg.Done()   
			c := f.getOrCreateChan(t)  
			for {  
				select {  
				case <-ctx.Done():  
					return   
				case event := <-c:  
					result <- event   
				}  
			}  
		}()  
	}  
  
	go func() {  
		wg.Wait() 
		close(result) 
	}()  
  
	return result, nil  
}

Activity

  1. reopened this on Feb 27, 2024
  2. changed the title [-]About the Implementation Logic of NewSourceTube in memory.go[/-] [+]Multiple closes on the same result channle in MemoryQueueFactory[/+] on Feb 28, 2024
  3. RobertIndie commented on Feb 28, 2024

    @RobertIndie
    Contributor

    Thanks for reporting. This should be a bug. Using WaitGroup looks good to me.

    Would you like to fix this issue?

  4. wyyolo commented on Feb 28, 2024

    @wyyolo
    ContributorAuthor

    Of course, I will fix this bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions