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

fix(gql_websocket_link): Fix race condition causing exception and double complete #471

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions links/gql_websocket_link/lib/src/graphql_transport_ws.dart
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,12 @@ class _ConnectionState {
retrying = false; // future lazy connects are not retries
retries = 0; // reset the retries on connect
final _completer = Completer<void>();
errorOrClosed(_completer.completeError);

errorOrClosed((error) {
if (_completer.isCompleted) return;
_completer.completeError(error);
});

connected(_Connected(
socket,
_completer.future,
Expand Down Expand Up @@ -974,7 +979,12 @@ class _Client extends TransportWsClient {

bool done = false;
bool errored = false;
bool released = false;

Function() releaser = () {
if (released) return;
released = true;

// for handling completions before connect
state.locks--;
done = true;
Expand Down Expand Up @@ -1018,8 +1028,15 @@ class _Client extends TransportWsClient {
// if not completed already and socket is open, send complete message to server on release
socket.sink.add(_completeMsg);
}
state.locks--;

done = true;

// Its possible for a CompleteMessage to be received during the await above
// and this code be run twice causing a double release issue.
if (released) return;
released = true;

state.locks--;
release();
};

Expand Down
Loading