Skip to content

Request callback/endOfMessage never triggering. #785

Closed

Description

@MaienM

We've run into a case where the statements of a query all complete, all data is received and processed, and the appropriate done* events are fired, but the callback given to request & the endOfMessage event are never fired.

This seems to exclusively happen when we use pause and resume. This only happens for a select few queries, so it's proven difficult to create a set for others to reproduce this with. It does appear to happen consistently for these queries, however.

The problem seems to occur when addEndOfMessageMarker in token-stream-parser.js is called while this.parser.suspend is true. In this case the _transform of stream-parser.js never receives this marker, and the event is thus never processed.

Any ideas on what may be causing this?

We're using tedious 2.1.5, but I've also reproduced this on the most recent version released on npm (2.6.4).

Activity

  1. Suraiya-Hameed commented on Sep 5, 2018

    @Suraiya-Hameed
    Member

    @MaienM Can you share the DML, DDL of the queries that consistently cause this issue?
    Pinging in @chdh who knows this area of code better.

  2. chdh commented on Sep 6, 2018

    @chdh
    Collaborator

    The only reason I can see, why the endOfMessageMarker pseudo message does not immediatelly reach the _transform() function of the token parser, is when the output queue of the transform is full (Transform._readableState.length >= Transform._readableState.highWaterMark) and the transform has started to queue input chunks. This can happen when data arrives faster from the SQL server than it is processed, or when the transform is in the paused state (Transform._readableState.flowing == false). In this case, the endOfMessageMarker pseudo message is queued within the input queue of the transform when addEndOfMessageMarker() calls Transform.write().

    But after the queued data has been processed, the endOfMessageMarker pseudo message should reach the _transform() function.

    @arthurschreiber Do you see a problem in our implementation of pause/resume?

  3. ErikJansenIRefact commented on Jan 7, 2019

    @ErikJansenIRefact

    We are now not only having problems with the endOfMessage event not being sent but are receiving a parse error now as well.

    'Error: Unknown type: 244\n at Parser.parseTokens (S:\\i-refact\\acme-js\\node_modules\\tedious\\lib\\token\\stream-parser.js:93:28)\n at Parser._transform (S:\\i-refact\\acme-js\\node_modules\\tedious\\lib\\token\\stream-parser.js:70:12)\n at Parser.Transform._read (S:\\i-refact\\acme-js\\node_modules\\tedious\\node_modules\\readable-stream\\lib\\_stream_transform.js:177:10)\n at Parser.Readable.read (S:\\i-refact\\acme-js\\node_modules\\tedious\\node_modules\\readable-stream\\lib\\_stream_readable.js:439:10)\n at flow (S:\\i-refact\\acme-js\\node_modules\\tedious\\node_modules\\readable-stream\\lib\\_stream_readable.js:890:34)\n at resume_ (S:\\i-refact\\acme-js\\node_modules\\tedious\\node_modules\\readable-stream\\lib\\_stream_readable.js:870:3)

    This error can be reproduced by retrieving a certain amount of rows for which we eventually call pause and resume.

    So we now seem to have 2 issues with the "streaming implementation" in Tedious.
    Could you please let us know if a fix is to be expected or how we should proceed?

  4. ErikJansenIRefact commented on Jan 7, 2019

    @ErikJansenIRefact

    Regarding the "unknown type" error: it seems to occur if we retrieve rows for which columns exists with lengthy string values (varchar(max)). Calling pause/resume seems to break the buffer somehow.
    Hope this helps.

  5. chdh commented on Jan 7, 2019

    @chdh
    Collaborator

    We can probably fix it when you provide a test case so that we can reproduce the problem.

  6. ErikJansenIRefact commented on Jan 8, 2019

    @ErikJansenIRefact

    We have a table and if we query it directly with periodic pause()/resume() calls it works fine.

    If we however retrieve this same table within our application we do get the parse error.
    Our application sends the row data to an internal transform stream. If this buffer is full we call the request.pause() function and upon the drain event we call request.resume(). So it seems our application does exactly the same as with our test setup. But eventually runs into a parse error.

    So we are trying to create a simple test setup where we can reproduce easily but this seems harder than expected.

    Perhaps you can give us some clues where to look for. It still seems that some kind of buffer gets corrupted.

  7. chdh commented on Jan 8, 2019

    @chdh
    Collaborator

    The byte stream, which gets parsed into tokens, could get interrupted when an ATTENTION message is sent. The client sends an ATTENTION when a timeout occurs or when the request is cancelled. Normally this is not a problem and parsing of the byte stream resumes correctly, but maybe there is something wrong. Maybe pause/resume only triggers the error.

  8. arthurschreiber commented on Jan 8, 2019

    @arthurschreiber
    Collaborator

    Yes, @chdh is right. I think you might be running into a request timeout, which causes the request to be cancelled via an attention event and the connection switches into the SENT_ATTENTION state, before the end-of-message marker can be pushed into the token stream parser.

    I'll see if I can come up with a fix. Thanks everyone for reporting this issue, and thanks for being patient with us to figure out what is going on! 🙇

  9. ErikJansenIRefact commented on Jan 8, 2019

    @ErikJansenIRefact

    Probably not an issue with timeouts. In the config we set "requestTimeout": 0.
    But it might be caused by a cancel on the request.

  10. chdh commented on Jan 9, 2019

    @chdh
    Collaborator

    I have made some extensive tests with large records (using varchar(max)), cancel and pause/resume. But I can't reproduce these two errors. The callback of the request is always called and a token parsing error did not occur.

    Which Tedious version do you use? Do you use an encrypted connection or anything special?

  11. chdh commented on Jan 10, 2019

    @chdh
    Collaborator

    I now managed to reproduce the problem of the missing request callback by extending my test program with random delays and large random data fields in the database. And I found the cause of the problems. It's a faulty program logic in the token stream parser transform. The error is not directly related with the pause/resume logic but it seems to get triggered by pause/resume.

    The root cause of the problem is that in the token parser transform, the callback routine of the _transform() method is called nested within the program logic. This leads to recursive invocation of the _transform() method within the callback call. Multiple recursive instances of the _transform() method are active on the same call stack and messing with each other's class variables.

    The test program is written in TypeScript, but I'm including the tempBuild directory with the generated JavaScript files.
    testcase.zip

  12. ErikJansenIRefact commented on Jan 10, 2019

    @ErikJansenIRefact

    Good to hear that you were able to reproduce and find the problem. We were still struggling getting a testcase together. Hopefully the PR will be accepted soon as this issue is currently blocking for us.

  13. arthurschreiber commented on Jan 10, 2019

    @arthurschreiber
    Collaborator

    @ErikJansenIRefact Would you mind taking the newly released v4.1.3 for a test drive, and see if that fixes the issues you're seeing?

    As it's still "hidden" via the next tag, you can install either via npm install tedious@next or npm install [email protected]

    Please let us know if that version fixes your issues, and we'll promote it to latest. 🙇

  14. ErikJansenIRefact commented on Jan 10, 2019

    @ErikJansenIRefact

    Hi Arthur, we tested the query and the change fixes the issue.
    Thanks Christian for the swift response and action.

  15. arthurschreiber commented on Jan 14, 2019

    @arthurschreiber
    Collaborator

    @ErikJansenIRefact Thanks for reporting back. I promoted v4.1.3 to the latest channel.

    @chdh Again, many thanks for looking into this! 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions