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

Restify 11.1.0 on Node.js 18.x Causes 500 Errors with “End of Handler Chain” After Cosmos DB Query #1979

Open
lt-dan457 opened this issue Sep 19, 2024 · 0 comments

Comments

@lt-dan457
Copy link

lt-dan457 commented Sep 19, 2024

Restify Version: 11.1.0
Node.js Version: 18.19.1 (also tested with 14.18.1)

Expected behaviour

After upgrading Restify from version 8.6.1 to 11.1.0 while running my project on Node.js 18.19.1, I expect Cosmos DB queries using container.items.query(querySpec).fetchAll() to function as they did previously. The API should return valid responses (200 or appropriate error codes like 204) without prematurely returning a 500 Internal Server Error. This setup worked without issues on Node.js 14.18.1 and Restify 8.6.1.

Actual behaviour

After migrating to Restify 11.1.0, I consistently encounter an Internal Server Error 500 triggered by Restify's internal Server._finishReqResCycle. The error occurs right after invoking fetchAll() for Cosmos DB queries. Despite logging showing the Cosmos query completes successfully (or returns empty results), Restify sends a 500 error prematurely before I can handle the response in my code. This occurs only after querying Cosmos DB; other routes that don’t rely on fetchAll() work as expected.

The output we see from the API call with the updated Restify version is:

Status 500 Internal Server Error
{
    "code": "InternalServer",
    "message": "reached the end of the handler chain without writing a response!"
}

Additionally, the issue persists even when:

  • Downgrading back to Node.js 14.18.1 while staying on Restify 11.1.0
  • Updating @azure/cosmos dependency to the latest version (4.1.1)
  • Refactoring the API call and ensuring proper use of async/await and next() throughout the middleware and route handlers
  • Adding a global unhandled rejection handler, which does not catch any relevant rejections

The issue appears related to how Restify is managing or finalizing the request lifecycle after an async operation.

Repro case

The issue manifests when querying Cosmos DB using @azure/cosmos (version 4.1.1). Here's the general flow:

Route handler is invoked
Cosmos DB is queried using container.items.query(querySpec).fetchAll()
Restify's Server._finishReqResCycle sends an Internal Server Error 500, seemingly before the query resolves or just after
The key point of failure is right after the Cosmos query execution (fetchAll()).

Cause

It appears that Restify is prematurely finalizing the request lifecycle when fetchAll() is called in an async context. This happens specifically after upgrading from Restify 8.6.1 to 11.1.0. It’s possible that this is related to changes in how Restify handles async/await in route handlers and middleware.

Are you willing and able to fix this?

If this issue is confirmed to be a bug within Restify, I’m happy to contribute to testing or helping with a fix, but I will need guidance from the Restify development team on where to start.

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

No branches or pull requests

1 participant