Skip to content

Unregister ConnectionWaiter listeners once connected#652

Merged
QuintinWillison merged 1 commit intomainfrom
feature/connection-waiter-cleanup
Mar 1, 2021
Merged

Unregister ConnectionWaiter listeners once connected#652
QuintinWillison merged 1 commit intomainfrom
feature/connection-waiter-cleanup

Conversation

@QuintinWillison
Copy link
Copy Markdown
Contributor

Fixes #651.

This is a relatively tentative fix in which I'm fairly confident due to the isolated and internal nature of the fix.

The fundamental issue was that we were not removing ConnectionWaiter instances as connection listeners, however I've also refactored the code a little for clarity.

There is probably plenty more I could have refactored but I wanted to keep the impact of this change minimal as we're aiming to get this out quickly as a bug fix.

…n token changes) when it falls out of scope.

Also refactors the code that calls this instance to make it more readable (avoiding use of continue, for example).
Copy link
Copy Markdown
Member

@paddybyers paddybyers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

* Remove this ConnectionWaiter as a connection listener.
*/
private void close() {
// This method is explicitly not synchronized. There may be a case for this in the
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this repo we've tended to use /* .... */ comment style

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For interface commentary I would agree, however within a method implementation I tend to prefer // as it's more compact - typically for single line comments but also for multi-line. I should document this somewhere.

private void close() {
// This method is explicitly not synchronized. There may be a case for this in the
// future, however its addition is designed to be lightweight with minimal impact.
if (closed) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding synchronized would be safe, but it this case it happens not to be necessary; if in the race the .off() is called twice, the second call would have no effect. Also, the calling code cannot end up calling close() more than once.

if (closed) {
return;
}
closed = true;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel closed=true can be set after connection.off(this);, that way it's logically closed after removing it from connection

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion, but I still prefer it this way around.

waitingForConnected = false;
break;

case connecting:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, but if it's connecting, it can transition to connected right ... that way it shouldn't break and just continue to check if the next state is connected

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add explicit continue ... WDYT

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure what you mean, as this whole switch statement is validating a single state. So, connected is transitory and connected will get encountered on a subsequent iteration of the while.

@QuintinWillison
Copy link
Copy Markdown
Contributor Author

Thanks all for the prompt reviews. 😁

@QuintinWillison QuintinWillison merged commit 9b51ad3 into main Mar 1, 2021
@QuintinWillison QuintinWillison deleted the feature/connection-waiter-cleanup branch March 1, 2021 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Many instances of ConnectionWaiter spawned while app is running, with authentication token flow

4 participants