Skip to content

req.socket being null, but external library might expect it do be defined #1115

@sveisvei

Description

@sveisvei

Bug Description

Experiencing req.socket being null: https://github.com/elastic/apm-agent-nodejs/blob/6a540301619ed21abb9f2accb8de98ba516f3531/lib/parsers.js#L23 and this code is throwing as a result.

"type": "TypeError",
"message": "Cannot read properties of null (reading 'remoteAddress')",
"stack":
    TypeError: Cannot read properties of null (reading 'remoteAddress')
        at Object.getContextFromRequest (/node_modules/.pnpm/elastic-apm-node@3.25.0/node_modules/elastic-apm-node/lib/parsers.js:28:34)
        at Transaction.toJSON (/node_modules/.pnpm/elastic-apm-node@3.25.0/node_modules/elastic-apm-node/lib/instrumentation/transaction.js:189:41)
        at Transaction._encode (/node_modules/.pnpm/elastic-apm-node@3.25.0/node_modules/elastic-apm-node/lib/instrumentation/transaction.js:218:15)
        at Instrumentation.addEndedTransaction (/node_modules/.pnpm/elastic-apm-node@3.25.0/node_modules/elastic-apm-node/lib/instrumentation/index.js:284:63)
        at Transaction.end (/node_modules/.pnpm/elastic-apm-node@3.25.0/node_modules/elastic-apm-node/lib/instrumentation/transaction.js:268:32)
        at ServerResponse.<anonymous> (/node_modules/.pnpm/elastic-apm-node@3.25.0/node_modules/elastic-apm-node/lib/instrumentation/http-shared.js:41:36)
        at ServerResponse.f (/node_modules/.pnpm/once@1.4.0/node_modules/once/once.js:25:25)
        at ServerResponse.onfinish (/node_modules/.pnpm/end-of-stream@1.4.4/node_modules/end-of-stream/index.js:31:27)
        at /node_modules/.pnpm/elastic-apm-node@3.25.0/node_modules/elastic-apm-node/lib/instrumentation/run-context/AbstractRunContextManager.js:76:49
        at AsyncHooksRunContextManager.with (/node_modules/.pnpm/elastic-apm-node@3.25.0/node_modules/elastic-apm-node/lib/instrumentation/run-context/BasicRunContextManager.js:49:17)
        at ServerResponse.wrapper (/node_modules/.pnpm/elastic-apm-node@3.25.0/node_modules/elastic-apm-node/lib/instrumentation/run-context/AbstractRunContextManager.js:76:23)
        at ServerResponse.emit (node:events:402:35)
        at ServerResponse.emit (node:domain:475:12)
        at onFinish (node:_http_outgoing:830:10)
        at afterWrite (node:internal/streams/writable:497:5)
        at afterWriteTick (node:internal/streams/writable:484:10)
        at processTicksAndRejections (node:internal/process/task_queues:82:21)

The offending line in undici, is.

stream.socket = null

Reproducible By

If e.g. https://github.com/elastic/apm-agent-nodejs hook into the request cycle after the socket has been destroyed, it will end up with a socket that is null, but this is something it does not except - and code will throw because it always expect socket to be defined.

Expected Behavior

I am not sure. I do expect us to not play with the raw socket reference in unexpected ways, or maybe ensure that we do it last.

Logs & Screenshots

Environment

16.13

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions