Skip to content

Commit

Permalink
fix kill query mismatch bug
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Dec 8, 2023
1 parent 7e40cc2 commit 63e705b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
18 changes: 9 additions & 9 deletions lib/DboBuilder/QueryStreamer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class QueryStreamer {
this.socketQueries[socketId]![id]!.client = currentClient;
try {
if(!client){
await currentClient.connect();
await currentClient.connect();
}
processID = (currentClient as any).processID
const queryStream = new QueryStream(query.query, undefined, { batchSize: 1e6, highWaterMark: 1e6, rowMode: "array" });
Expand Down Expand Up @@ -167,11 +167,12 @@ export class QueryStreamer {
const streamResult = getStreamResult();
streamState = "ended";
emit("ended", streamResult);
// release the client when the stream is finished AND connection is not persisted
if(!query.options?.persistStreamConnection){
delete this.socketQueries[socketId]?.[id];
currentClient.end();

if(query.options?.persistStreamConnection){
return;
}
delete this.socketQueries[socketId]?.[id];
currentClient.end();
});
} catch(err){
socketQuery.onError(err);
Expand All @@ -189,7 +190,7 @@ export class QueryStreamer {
if(!queryClient) return;
try {
const stopFunction = opts?.terminate? "pg_terminate_backend" : "pg_cancel_backend";
const rows = await this.adminClient.query(`SELECT ${stopFunction}(pid), pid, state, query FROM pg_stat_activity WHERE pid = $1 AND query = $2`, [processID, query.query]);
const rows = await this.adminClient.query(`SELECT ${stopFunction}(pid), pid, state, query FROM pg_stat_activity WHERE pid = $1`, [processID]);
cleanup()
cb({ processID, info: rows.rows[0] });
} catch (error){
Expand All @@ -204,12 +205,11 @@ export class QueryStreamer {
socket.removeAllListeners(channel);
socket.on(channel, async (_data: { query: string; params: any } | undefined, cb: BasicCallback) => {
if(streamState === "started"){
// TODO
return cb(null, "Already started");
}
streamState = "started";
try {
/* Query for persisted connection */
/* Persisted connection query */
if(runCount){
const persistedClient = this.socketQueries[socketId]?.[id];
if(!persistedClient) throw "Persisted query client not found";
Expand All @@ -226,7 +226,7 @@ export class QueryStreamer {
runCount++;
});

/** If not started in 5 seconds then assume this will never happen */
/** If not started within 5 seconds then assume it will never happen */
setTimeout(() => {
if(streamState) return;
cleanup();
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prostgles-server",
"version": "4.1.140",
"version": "4.1.141",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion tests/server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 63e705b

Please sign in to comment.