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

Crash during sql json return from SQL #16790

Open
haiodo opened this issue Jan 27, 2025 · 3 comments
Open

Crash during sql json return from SQL #16790

haiodo opened this issue Jan 27, 2025 · 3 comments
Assignees
Labels
crash An issue that could cause a crash macOS An issue that occurs on macOS

Comments

@haiodo
Copy link

haiodo commented Jan 27, 2025

How can we reproduce the crash?

No response

Relevant log output

Stack Trace (bun.report)

Bun v1.2.0 (b0c5a76) on macos aarch64 [RunCommand]

Segmentation fault at address 0x00000005

  • 2 unknown/js code
  • JSC::Stringifier::Holder::appendNextProperty
  • JSC::Stringifier::appendStringifiedValue
  • JSC::Stringifier::stringify
  • JSC::stringify
  • JSC__JSValue__jsonStringify
  • ResponseClass__constructJSON
  • 1 unknown/js code
  • jsc_llint_commonCallOp__llintOpWithMetadata__llintOpWithReturn__llintOp__commonOp__fn__fn__makeReturn__fn__fn__fn__666_callHelper__dispatch_LowLevelInterpreter64_asm_2538

Features: process_dlopen, http_server, jsc, tsconfig, tsconfig

Sentry Issue: BUN-BYC

@haiodo haiodo added the crash An issue that could cause a crash label Jan 27, 2025
@github-actions github-actions bot added the macOS An issue that occurs on macOS label Jan 27, 2025
@Jarred-Sumner
Copy link
Collaborator

Can you paste the line of code that calls Response.json? Initially, I thought it was a missing exception check but we do check for exceptions in that code and we check for null strings. So it's something that is crashing when Response.json() is called with it, but it's not clear what that is.

@haiodo
Copy link
Author

haiodo commented Jan 27, 2025

Hi @Jarred-Sumner, all my scenario is following:

import { postgres, type SQL } from 'bun'
const port = process.env.PORT ?? 6767
const version = process.env.VERSION ?? '0.6.388'
const dbUrl = process.env.DB_URL ?? 'postgresql://[email protected]:26257/defaultdb?sslmode=disable'
const extraOptions = JSON.parse(process.env.DB_OPTIONS ?? '{}')

const authToken = process.env.AUTH_TOKEN ?? 'secret'

const sql: SQL = new postgres(dbUrl, {
  ...extraOptions
})

Bun.serve({
  async fetch(req: Request) {
    const token = (req.headers.get('Authorization') ?? '').split(' ')[1]
    if (token !== authToken) {
      return new Response('Unauthorized', { status: 401 })
    }

    const url = new URL(req.url)
    if (url.pathname.startsWith('/api/v1/version')) {
      return new Response(version)
    }

    if (req.method === 'POST' && url.pathname.startsWith('/api/v1/sql')) {
      const json = await req.json()

      const to = setTimeout(() => {
        console.error('timeout', json.query, json.params)
      }, 10000)
      try {
        const result = await sql(json.query, ...(json.params ?? []))
        console.log('result', json.query, result.length)
        return Response.json(result)
      } catch (err: any) {
        console.error('failed to execute sql', json.query, json.params, err.message, err)
        return new Response(err.message, { status: 500 })
      } finally {
        clearTimeout(to)
      }
    }

    return new Response('Success!')
  },
  port
})

May be my SQL result is not fine.

@Jarred-Sumner
Copy link
Collaborator

Thanks. It's definitely an issue in Bun and not in your code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash An issue that could cause a crash macOS An issue that occurs on macOS
Projects
None yet
Development

No branches or pull requests

3 participants