FetchError: request to https://db.fauna.com/ failed, reason: write EPIPE

I’m trying to launch my new web app using Netlify Functions accessing FaunaDB and while it seems to work locally great (via netlify dev), when it’s online it feels like every 3rd call to the database fails with the following error:

FetchError: request to https://db.fauna.com/ failed, reason: write EPIPE
at ClientRequest.<anonymous> (/var/task/src/node_modules/faunadb/node_modules/node-fetch/lib/index.js:1461:11)
at ClientRequest.emit (events.js:315:20)
at TLSSocket.socketErrorListener (_http_client.js:426:9)
at TLSSocket.emit (events.js:315:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  type: 'system',
  errno: 'EPIPE',
  code: 'EPIPE'
}

I have no clue what this means or what I’m maybe doing wrong or how to fix it. It seems like it’s on Fauna’s side.

All of the “server side code” executing calls to Fauna are in various Netlify functions (which I guess are just wrapped up AWS Lambdas). Various different function calls hitting different Netlify functions are generating this error, not just one in particular.

Answer

The error that you are receiving may be caused by an issue similar to querying FaunaDB from AWS Lambda functions: https://docs.fauna.com/fauna/current/drivers/known_issues

Essentially, the Netlify function context that support your function might not always be running. If that’s the case, the connection state stored within the FaunaDB driver’s client object might be stale when the execution context is restored and your next query is attempted.

The typical solution is to create the client object, and perform your queries, in the main handler logic, rather than in the initialization logic.

I don’t know whether this answer is helpful for your situation. If it is, let me know and we can update the guidance in Fauna documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *