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

native client listen does not call the wg.Done if the context is cancelled (prior to a DialConfig success). Here is code to resolve the problem. #145

Open
daninmadison opened this issue Feb 13, 2023 · 1 comment

Comments

@daninmadison
Copy link
Contributor

In the listen function, the for loop checks for a ctx closed. If it is, it exits the function.
It needs to call the wg.Done.

If the Connect function is given a wrong details, the initial listen will never succeed the websocket.DialConfig.
As a result, the only way to stop this function is to cancel the context.
The problem is, the context closed does not call wg.Done prior to the return.

As a result, the Connect will be stuck at the wg.Wait and never return.

Here is code to correct the problem.

func (c *Client) listen(ctx context.Context, wg *sync.WaitGroup) {
var signalUp sync.Once

for {
	// Exit if our context has been closed
	if ctx.Err() != nil {
		if wg != nil {
			wg.Done()
		}
		return
	}
@daninmadison
Copy link
Contributor Author

daninmadison commented Feb 16, 2023

I realized the code I pushed earlier this week had a bug.
It needs to use the sync once signalUp for the wg.Done.

Pushed a new fix. Just in case that didn't come through correctly, here is the corrected code...

for {
	// Exit if our context has been closed
	if ctx.Err() != nil {
		if wg != nil {
			signalUp.Do(wg.Done)
		}
		return
	}

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

No branches or pull requests

1 participant