Skip to content

Commit

Permalink
fix: Don't close client if we've already aborted (#968)
Browse files Browse the repository at this point in the history
  • Loading branch information
bouk authored and stephenh committed Nov 28, 2023
1 parent 105e5fd commit 7ee1507
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
18 changes: 12 additions & 6 deletions integration/grpc-web-abort-signal/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1081,17 +1081,23 @@ export class GrpcWebImpl {
}
},
});
observer.add(() => {
if (!abortSignal || !abortSignal.aborted) {
return client.close();
}
});

if (abortSignal) {
abortSignal.addEventListener("abort", () => {
const abort = () => {
observer.error(abortSignal.reason);
client.close();
};
abortSignal.addEventListener("abort", abort);
observer.add(() => {
if (abortSignal.aborted) {
return;
}

abortSignal.removeEventListener("abort", abort);
client.close();
});
} else {
observer.add(() => client.close());
}
});
upStream();
Expand Down
4 changes: 1 addition & 3 deletions integration/grpc-web/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1028,9 +1028,7 @@ export class GrpcWebImpl {
}
},
});
observer.add(() => {
return client.close();
});
observer.add(() => client.close());
});
upStream();
}).pipe(share());
Expand Down
39 changes: 22 additions & 17 deletions src/generate-grpc-web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,6 @@ function createInvokeMethod(ctx: Context) {
const { options } = ctx;
const { useAbortSignal } = options;

const maybeAbortSignal = useAbortSignal
? `
if (abortSignal) abortSignal.addEventListener("abort", () => {
observer.error(abortSignal.reason);
client.close();
});`
: "";

return code`
invoke<T extends UnaryMethodDefinitionish>(
methodDesc: T,
Expand Down Expand Up @@ -380,16 +372,29 @@ function createInvokeMethod(ctx: Context) {
}
},
});
observer.add(() => {
${
!useAbortSignal
? `return client.close();`
: `if (!abortSignal || !abortSignal.aborted)
return client.close();`
}
});
${
useAbortSignal
? `
if (abortSignal) {
const abort = () => {
observer.error(abortSignal.reason);
client.close();
};
abortSignal.addEventListener("abort", abort);
observer.add(() => {
if (abortSignal.aborted) {
return;
}
${maybeAbortSignal}
abortSignal.removeEventListener('abort', abort);
client.close();
});
} else {
observer.add(() => client.close());
}
`
: `observer.add(() => client.close());`
}
});
upStream();
}).pipe(${share}());
Expand Down

0 comments on commit 7ee1507

Please sign in to comment.