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

bg: ensure init is complete before returning #216

Merged
merged 3 commits into from
May 2, 2022
Merged

bg: ensure init is complete before returning #216

merged 3 commits into from
May 2, 2022

Conversation

rusq
Copy link
Contributor

@rusq rusq commented Apr 23, 2022

I encountered an issue, where i started Auth immediately after calling Connect, and it seems that the Auth kicked in before the client.Run has finished initialisation.

The code that revealed the problem:

// for context
type Client {
	cl *telegram.Client
	auth auth.UserAuthenticator
	sendcodeOpts auth.SendCodeOptions
}

func (c *Client) Start(ctx context.Context) error {
// <...>

	stop, err := bg.Connect(c.cl)
	if err != nil {
		return err
	}

	// time.Sleep(500*time.Millisecond) // works around the issue

	flow := auth.NewFlow(c.auth, c.sendcodeOpts)
	if err := c.cl.Auth().IfNecessary(ctx, flow); err != nil {
		return err
	}
	log.Println("auth success")

}

To confirm this theory, I added a time.Sleep(500*time.Millisecond) after Connect, and before Auth (as shown on the commented line above)

Introducing the synchronisation in Connect fixes the described problem.

@rusq
Copy link
Contributor Author

rusq commented Apr 26, 2022

FIXED: If the Run errors out, the error is not returned until the wg.Wait() returns it, and wg.Wait is called only in Stop(), which is usually deferred. Consider the following example:

With (c *Client) defined as:

type Client struct {
  cl *telegram.Client
  auth         auth.UserAuthenticator
  sendcodeOpts auth.SendCodeOptions
}
// func (c *Client) Run() error {
stop, err := bg.Connect(c.cl)
if err != nil {
  return err
}
defer func() {
  if err := stop(); err!=nil {
    log.Print(err)
  }
}()
// even if Run fails with error, we will reach this point


flow := auth.NewFlow(c.auth, c.sendcodeOpts)

// *** the following will hang indefinitely ***
if err := c.cl.Auth().IfNecessary(ctx, flow); err != nil {
  return err
}

log.Debug("auth success")

return nil
}

@codecov
Copy link

codecov bot commented May 2, 2022

Codecov Report

Merging #216 (cea888c) into master (9a5b21a) will increase coverage by 16.39%.
The diff coverage is n/a.

@@             Coverage Diff             @@
##           master     #216       +/-   ##
===========================================
+ Coverage   55.47%   71.87%   +16.39%     
===========================================
  Files          48        9       -39     
  Lines        1871      256     -1615     
===========================================
- Hits         1038      184      -854     
+ Misses        752       51      -701     
+ Partials       81       21       -60     
Impacted Files Coverage Δ
bg/connect.go
bbolt/auth.go
invoker/debug.go
auth/terminal/code.go
storage/find.go
auth/localization/catalog.go
pebble/peer_storage.go
middleware/floodwait/queue.go
invoker/update.go
storage/peer_as.go
... and 29 more

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

@ernado ernado merged commit 0800e57 into gotd:master May 2, 2022
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

Successfully merging this pull request may close these issues.

2 participants