Skip to content
Open
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions packages/fxa-settings/src/lib/channels/pairing-channel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ let mockChannel: {
removeEventListener: jest.Mock;
_channelId?: string;
_channelKey?: Uint8Array;
_connection?: unknown;
};

jest.mock(
Expand Down Expand Up @@ -250,6 +251,23 @@ describe('PairingChannelClient', () => {
await expect(client.close()).resolves.toBeUndefined();
expect(client.isConnected).toBe(false);
});

// A socket error disconnects the channel without a matching `close` event,
// and the integrations tear down in response — so close() runs against a
// channel the package has already unwired.
it('skips the underlying close once the socket is gone', async () => {
await client.open(SERVER, CHAN, VALID_KEY);
getMockHandler('error')(
new CustomEvent('error', { detail: new Error('ws fail') })
);
mockChannel._connection = null;

await expect(client.close()).resolves.toBeUndefined();

expect(mockChannel.close).not.toHaveBeenCalled();
expect(mockChannel.removeEventListener).toHaveBeenCalled();
expect(client.isConnected).toBe(false);
});
});

describe('incoming messages', () => {
Expand Down
15 changes: 15 additions & 0 deletions packages/fxa-settings/src/lib/channels/pairing-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ type PairingChannelSocket = {
removeEventListener(type: string, listener: EventListener): void;
_channelId?: string;
_channelKey?: Uint8Array;
/**
* fxa-pairing-channel's own handle on the socket. It is nulled out when the
* socket goes away, and the package's `close()` dereferences it unguarded.
*/
_connection?: unknown;
};

export class PairingChannelClient extends EventTarget {
Expand Down Expand Up @@ -275,6 +280,16 @@ export class PairingChannelClient extends EventTarget {
const ch = this.channel;
this.channel = null;
this.removeChannelListeners(ch);

// An `error` from the socket leaves the channel disconnected without a
// matching `close`, so teardown still arrives here holding a dead channel.
// There is no socket left to shut down, and asking the package to close one
// throws. An absent `_connection` is not the same signal as a null one:
// only null means the package has torn the socket down.
if (ch._connection === null) {
return;
}

try {
console.info('Closing channel ', ch._channelId)
await ch.close();
Expand Down