Skip to content

Conversation

@ChaituVR
Copy link
Member

@ChaituVR ChaituVR commented Aug 15, 2025

Issue

Timeout for quires doesn't work, this keep the connections open for long durations than we expect them to

Summary

Adds automatic 30-second timeout to all database queries to prevent long-running queries from hanging indefinitely.

Changes

  • Override queryAsync method in mysql.ts to automatically add timeout to all queries
  • No changes needed to existing code - all queries automatically get timeout protection

How to Test

  1. Change QUERY_TIMEOUT_MS from 30000 to 5000 in src/helpers/mysql.ts
  2. Run a query that takes longer than 5 seconds
curl 'http://localhost:3000/graphql?' \
  -H 'Accept: application/json' \
  --data-raw $'{"query":"\\n  query GetMessages($space: String\u0021, $first: Int\u0021, $skip: Int\u0021, $timestamp_lt: Int) {\\n    messages(\\n      first: $first\\n      skip: $skip\\n      where: {\\n        space: $space\\n        type_in: [\\"proposal\\", \\"settings\\", \\"delete-proposal\\", \\"update-proposal\\"]\\n        timestamp_lt: $timestamp_lt\\n      }\\n      orderBy: \\"timestamp\\"\\n      orderDirection: desc\\n    ) {\\n      id\\n      mci\\n      type\\n      ipfs\\n      timestamp\\n\\n    }\\n  }","variables":{"space":"gitcoindao.eth","first":100,"skip":0},"operationName":"GetMessages"}'
  1. Verify the query times out after 5 seconds instead of hanging

@ChaituVR ChaituVR changed the title Add 30-second timeout to all database queries fix: timeout to all database queries Aug 16, 2025
@ChaituVR ChaituVR marked this pull request as ready for review August 16, 2025 10:58
Comment on lines +43 to +50
const originalQueryAsync = hubDB.queryAsync;
hubDB.queryAsync = function (sql: string, values?: any) {
return originalQueryAsync.call(this, {
sql: sql,
values: values,
timeout: QUERY_TIMEOUT_MS
});
};
Copy link
Member

Choose a reason for hiding this comment

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

Can't you simplify like this?

Suggested change
const originalQueryAsync = hubDB.queryAsync;
hubDB.queryAsync = function (sql: string, values?: any) {
return originalQueryAsync.call(this, {
sql: sql,
values: values,
timeout: QUERY_TIMEOUT_MS
});
};
hubDB.queryAsync = (sql: string, values?: any) => hubDB.queryAsync.call(this, {
sql,
values,
timeout: QUERY_TIMEOUT_MS
});

Copy link
Member

@bonustrack bonustrack Aug 16, 2025

Choose a reason for hiding this comment

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

That may break the this actually. But dont need to repeat sql and values, or doing a const for queryAsync

@bonustrack
Copy link
Member

There isn't a more native way to do it without rewriting query? Did you find this trick somewhere?

@ChaituVR
Copy link
Member Author

ChaituVR commented Aug 17, 2025

There isn't a more native way to do it without rewriting query? Did you find this trick somewhere?

I found this here https://www.npmjs.com/package/mysql#timeouts https://discord.com/channels/955773041898573854/1403444404295045141/1405865171506696254

If not we may have to pass it to each query individually, so we overwrite queryAsync

@Sekhmet
Copy link
Member

Sekhmet commented Aug 18, 2025

I think this doesn't cancel query on DB itself. Is this aimed at just long queries or is there other usecase? Because if we add this kind of timeout it would basically kill long queries on the API while still being an issue on the DB itself (so we just waste resources without giving response to the client).

@ChaituVR
Copy link
Member Author

Hmm yes we need a timeout on DB https://discord.com/channels/955773041898573854/1403444404295045141/1406532788194250872

With this PR, it fixes the earlier timeout which was not working, this change will prevent long-running connections on server, also there is some improvement on DB
https://discord.com/channels/955773041898573854/1403444404295045141/1406311212567892079
we can go with this PR if there is no way to add timeout on DB

@wa0x6e
Copy link
Contributor

wa0x6e commented Aug 19, 2025

If the goal is to prevent long running connections, how about just adding a timeout on the express js side ? https://expressjs.com/en/resources/middleware/timeout.html

@ChaituVR
Copy link
Member Author

If the goal is to prevent long running connections, how about just adding a timeout on the express js side ? https://expressjs.com/en/resources/middleware/timeout.html

Nice yea, we should also add some timeout like this, does it also stop network requests/DB connections on timeout?

@wa0x6e
Copy link
Contributor

wa0x6e commented Aug 20, 2025

If the goal is to prevent long running connections, how about just adding a timeout on the express js side ? https://expressjs.com/en/resources/middleware/timeout.html

Nice yea, we should also add some timeout like this, does it also stop network requests/DB connections on timeout?

Nope, it just releases the resource by closing the http request, which is essentially the same feature as this PR, since it's also not killing the long running query

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants