surrealdb / surrealdb.js

SurrealDB SDK for JavaScript
https://surrealdb.com
Apache License 2.0
270 stars 46 forks source link

Bug: EngineDisconnet if I access the database too fast... #293

Open irishburlybear opened 4 days ago

irishburlybear commented 4 days ago

Describe the bug

I get this error if there are too many queries happening I guess. (svelte) I have a hooks accessing the info for the user.

EngineDisconnected: The engine reported the connection to SurrealDB has dropped at file:///home/gray/programs/timeclock/node_modules/surrealdb.js/esm/surreal.js:116:50 at Array.map () at Surreal.clean (file:///home/gray/programs/timeclock/node_modules/surrealdb.js/esm/surreal.js:116:17) at Surreal.close (file:///home/gray/programs/timeclock/node_modules/surrealdb.js/esm/surreal.js:108:14) at Surreal.connect (file:///home/gray/programs/timeclock/node_modules/surrealdb.js/esm/surreal.js:73:20) at Object.handle (/home/gray/programs/timeclock/src/hooks.server.js:57:11) at Module.respond (/home/gray/programs/timeclock/node_modules/@sveltejs/kit/src/runtime/server/respond.js:319:40) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async file:///home/gray/programs/timeclock/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:524:22

Steps to reproduce

In sveltekit, have hooks.server.js get the current user info, then do a db query immediately after.

Expected behaviour

Not to be disconnected from the engine...

SurrealDB version

1.5.3

SurrealDB.js version

beta.9

Contact Details

irishburlybear@gmail.com

Is there an existing issue for this?

Code of Conduct

kearfy commented 4 days ago

Hey @irishburlybear, this error is sent to all pending requests if you close the connection before they can be completed. Are you sure that you await the info call? It would help out a lot if you could send over a reproducable example

irishburlybear commented 4 days ago

hooks.server.js: Notice I am using ws because http didn't work / doesn't work..

await db.connect('ws://127.0.0.1:8000/rpc')
    await db.authenticate(session)

    event.locals.user = await db.info()
    event.locals.db = db

    return await resolve(event);
irishburlybear commented 4 days ago

It will run once, but if I refresh to generate another pdf, I get the above error...

import { printRegularLabel } from '$lib/reports';
import { RecordId } from 'surrealdb.js';

export const actions = {
    print_label: async ({ request, locals, params }) => {
        const data = Object.fromEntries(await request.formData());
        console.log(params, data);

        try {
            if (data.print_type === 'reg') {
                const employee = await locals.db.select(new RecordId('employees', params.id));

                console.log('print employee:', employee);

                return await printRegularLabel(employee);
            }
        } catch (err) {
            console.log(err.message);
        }
    }
};
irishburlybear commented 4 days ago

BTW, I'm using a query because I couldn't use select. But maybe because RecordID, I'm doing it wrong?

Update! I rewrote select to use the RecordID. Still has the same issue.

irishburlybear commented 4 days ago

Here is the function printRegualLabel():

import PdfPrinter from 'pdfmake';

import { v4 as uuid } from 'uuid';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';

export async function printRegularLabel(employee) {

    const fonts = {
        Roboto: {
          normal: 'src/lib/fonts/roboto/Roboto-Regular.ttf',
          bold: 'src/lib/fonts/roboto/Roboto-Bold.ttf'
        }
      }

    const dd = new PdfPrinter(fonts)

    let pdf = {
        pageSize: 'A4',
        pageOrientation: 'portrait',
        pageMargins: [40, 60, 40, 20],
        content: [
            {text: employee.first+' '+employee.last, fontSize: 12, margin: [0,2] },
            {qr: employee.ulid, fit: '100', eccLevel: 'H'}
        ]
    };

    let filename = uuid();
    const dir = path.join(os.tmpdir(), `${filename}.pdf`);

    try {
        let pdfDoc = dd.createPdfKitDocument(pdf);
        pdfDoc.pipe(fs.createWriteStream(dir));
        pdfDoc.end();

        return filename;
    } catch (err) {
        console.log(err);
    }
}
irishburlybear commented 4 days ago

I also tried adding await locals.db.close() at the end of the actions print_label: Still crashes after about 3 refreshes..

irishburlybear commented 4 days ago

Here is the new output after switching to http protocol:

Getting PDF is the filename I create in the tmp/os folder for it to be downloaded The data here is all dummy data...

Getting pdf... 716625ec-c30e-44bc-9d96-acd3e3a49abc { id: 'yf5524ihygkf9s0t6ge9' } { print_type: 'reg' } print employee: { active: true, first: 'Jesse', id: RecordId { tb: 'employees', id: 'yf5524ihygkf9s0t6ge9' }, last: 'Cruncher', simple_id: 6795, ulid: '01J0HMNPDYFW3WZ7PADJ3M0WXJ' } Search: null Getting pdf... 2da0be34-1bca-4980-942b-b0387cad2fbd { id: 'yf5524ihygkf9s0t6ge9' } { print_type: 'reg' } print employee: { active: true, first: 'Jesse', id: RecordId { tb: 'employees', id: 'yf5524ihygkf9s0t6ge9' }, last: 'Cruncher', simple_id: 6795, ulid: '01J0HMNPDYFW3WZ7PADJ3M0WXJ' } ConnectionUnavailable: There is no connection available at this moment. at HttpEngine.rpc (file:///home/gray/programs/timeclock/node_modules/surrealdb.js/esm/library/engine.js:223:19) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async Surreal.info (file:///home/gray/programs/timeclock/node_modules/surrealdb.js/esm/surreal.js:168:21) at async Object.handle (/home/gray/programs/timeclock/src/hooks.server.js:66:22) at async Module.respond (/home/gray/programs/timeclock/node_modules/@sveltejs/kit/src/runtime/server/respond.js:319:20) at async file:///home/gray/programs/timeclock/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:524:22 Getting pdf... 8f709fb3-3b0d-4e40-8429-6df0fbe97ddc