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

SQL from bun gets timeouts very easly #16892

Open
belizariogr opened this issue Jan 30, 2025 · 15 comments · Fixed by #16854 · May be fixed by #16984
Open

SQL from bun gets timeouts very easly #16892

belizariogr opened this issue Jan 30, 2025 · 15 comments · Fixed by #16854 · May be fixed by #16984
Labels
bug Something isn't working sql Something to do with `sql` in the "bun" module

Comments

@belizariogr
Copy link

belizariogr commented Jan 30, 2025

What version of Bun is running?

1.2.1

What platform is your computer?

Windows x64, Linux x64

What steps can reproduce the bug?

Using the SQL from "bun", the server hangs and returns:

Idle timeout reached after 5s

changing the driver for the "postgres" (that the native driver is based on) the problem does not happening.

import { Elysia } from 'elysia'
import { SQL } from 'bun'
import postgres from 'postgres'

const sql = new SQL({ url: process.env.DATABASE_URL, max: 10, idleTimeout: 5 });

// const sql = postgres(process.env.DATABASE_URL, { max: 10, idle_timeout: 5});


new Elysia({ serve: { idleTimeout: 10 } })
    .get('/', async () => {
        const res = await sql`SELECT * FROM cidades`;
        return { rows: res };
    })
    .listen(3000, () => {
        console.log('Server is running on port 3000');
    });

Here is a video showing the issue happening.

https://youtu.be/tjbe4-xixUk

What is the expected behavior?

Return the records all the time.

What do you see instead?

Hangs and gets timeout.

Additional information

Tested on Windows x64 and Linux (Ubuntu 24.04), PostgreSQL 17.

@belizariogr belizariogr added bug Something isn't working needs triage labels Jan 30, 2025
@RiskyMH RiskyMH added sql Something to do with `sql` in the "bun" module and removed needs triage labels Jan 30, 2025
@belizariogr
Copy link
Author

Just to let you know... the issue still persists on 1.2.2.

@RiskyMH @Jarred-Sumner @cirospaciari

@cirospaciari
Copy link
Member

cirospaciari commented Feb 2, 2025

Just to let you know... the issue still persists on 1.2.2.

@RiskyMH @Jarred-Sumner @cirospaciari

Can you send a repro?
Im trying the following code:

import { Elysia } from "elysia";
import { SQL } from "bun";

const sql = new SQL({
  url: "postgresql://bun:bunbunbun@localhost:5432/bun",
  idleTimeout: 5,
});

new Elysia({ serve: { idleTimeout: 10 } })
  .get("/", async () => {
    const res = await sql`SELECT * FROM "users" limit 1`;
    return { rows: res };
  })
  .listen(3000, () => {
    console.log("Server is running on port 3000");
  });
await fetch("http://localhost:3000/")
  .then(res => res.json())
  .then(console.log);

await Bun.sleep(10_000);
await fetch("http://localhost:3000/")
  .then(res => res.json())
  .then(console.log);

Output using bun --revision 1.2.2+c1708ea6a:

Image

Output using bunx [email protected] test.mjs or bunx [email protected] test.mjs

Image Image

@RiskyMH RiskyMH reopened this Feb 2, 2025
@RiskyMH RiskyMH closed this as completed Feb 2, 2025
@belizariogr
Copy link
Author

Just one file... using the simple code:

import { Elysia } from 'elysia'
import { SQL } from 'bun'
// import postgres from 'postgres'

const sql = new SQL({ url: process.env.DATABASE_URL, max: 10, idleTimeout: 5 });

// const sql = postgres(process.env.DATABASE_URL, { max: 10, idle_timeout: 5});


new Elysia({ serve: { idleTimeout: 10 } })
    .get('/', async () => {
        const res = await sql`SELECT * FROM cidades`;
        return { rows: res };
    })
    .listen(3000, () => {
        console.log('Server is running on port 3000');
    });

I just tested on Linux as well... same result.

Here is a new video showing the case.

https://youtu.be/m8YBA96rlPY

@belizariogr
Copy link
Author

And I dont know if is important or not... but, the reuslt set is kind a big...

Image

@belizariogr
Copy link
Author

belizariogr commented Feb 2, 2025

And, another information that I belive may be relevant... using K6, before, I use to get alot of timeouts... now, I get none... but, if I send the request slowly like in the video.... the problem happens.

@cirospaciari
Copy link
Member

@belizariogr running on Windows, Linux or MacOS I cannot reproduce this when using neon.tech or localhost databases even if I return 100k users, what service are you using for the database? Wondering if is some IPV6/IPV4 problem

@belizariogr
Copy link
Author

Im running this also in localhost.

@cirospaciari
Copy link
Member

Im running this also in localhost.

You are using Postgres 17 right? maybe this is the problem Im testing with 14, 15 and 16 will try with 17 tomorrow will reopen the Issue and investigate further

@cirospaciari cirospaciari reopened this Feb 2, 2025
@belizariogr
Copy link
Author

And, if was a IPv6/v4 problem of some sort... the problem should be happing on the posgres driver as well.

@belizariogr
Copy link
Author

Yes... Postgres 17.

@RiskyMH
Copy link
Member

RiskyMH commented Feb 2, 2025

can confirm, with lots of data Bun.sql times out but postgres.sql works

@cirospaciari
Copy link
Member

Yes... Postgres 17.

Ok got the repro will work on this tomorrow, Thank you @belizariogr

@belizariogr
Copy link
Author

Just tested on Postgres 16... and have the same result... but, limiting the results on 1 record... no issues... The problem is related with large result set.... apparently.

@belizariogr
Copy link
Author

Testing here... the problem only happens when the result set is more than 100 KB.... If is less than that... no issues.

@RiskyMH
Copy link
Member

RiskyMH commented Feb 2, 2025

Testing here... the problem only happens when the result set is more than 100 KB.... If is less than that... no issues.

this makes sense as even if you increase timeout to something huge, it never actually finishes.

@cirospaciari cirospaciari linked a pull request Feb 2, 2025 that will close this issue
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working sql Something to do with `sql` in the "bun" module
Projects
None yet
3 participants