8000 Fix TypeError when a connection error occurs with Streams by mmollick · Pull Request #6197 · knex/knex · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix TypeError when a connection error occurs with Streams #6197

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mmollick
Copy link
@mmollick mmollick commented Apr 14, 2025

Encountered a TypeError when dealing with a failed connection attempt when using Knex's streams. This introduces a check in releaseConnection to ensure the connection is set.

/Users/mmollick/repos/knex-repro/node_modules/knex/lib/client.js:344
    debug('releasing connection to pool: %s', connection.__knexUid);
                                                         ^

TypeError: Cannot read properties of undefined (reading '__knexUid')
    at Client_PG.releaseConnection (/Users/mmollick/repos/knex-repro/node_modules/knex/lib/client.js:344:58)
    at Transform.<anonymous> (/Users/mmollick/repos/knex-repro/node_modules/knex/lib/execution/runner.js:72:19)
    at Transform.emit (node:events:530:35)
    at emitCloseNT (node:internal/streams/destroy:148:10)
    at emitErrorCloseNT (node:internal/streams/destroy:130:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:90:21)

Reproduction

const knex = require('knex')({
    client: 'pg',
    connection: 'postgres:///knex_test' // Non existent database - connection will fail
});

async function mainNoStream() {
    try {
        const rows = await knex.table('users');

        for (const row of rows) {
            console.log(row);
        }
    } catch (err) {
        // Expected: Caught: Error: SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string
        console.error('NO STREAM CAUGHT:', err.message);
    }
}

async function mainWithStream() {
    try {
        const stream = knex.table('users').stream();

        for await (const row of stream) {
            console.log(row);
        }
    } catch (err) {

        // Expected: Caught: Error: SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string
        console.error('STREAM CAUGHT:', err.message);
    }
}


const main = async () => {
    // 
    await mainNoStream();

    // Unexpected TypeError thrown
    await mainWithStream();
}

main();

@mmollick mmollick changed the title check for connection before closing on connection error Fix TypeError when a connection error occurs with Streams Apr 14, 2025
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.

1 participant
0