You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Request callback/endOfMessage never triggering. #785
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).
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?
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?
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.
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.
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.
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! 🙇
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?
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
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.
Activity
Suraiya-Hameed commentedon Sep 5, 2018
@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.
chdh commentedon Sep 6, 2018
The only reason I can see, why the
endOfMessageMarkerpseudo 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, theendOfMessageMarkerpseudo message is queued within the input queue of the transform whenaddEndOfMessageMarker()callsTransform.write().But after the queued data has been processed, the
endOfMessageMarkerpseudo message should reach the_transform()function.@arthurschreiber Do you see a problem in our implementation of pause/resume?
ErikJansenIRefact commentedon Jan 7, 2019
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?
ErikJansenIRefact commentedon Jan 7, 2019
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.
chdh commentedon Jan 7, 2019
We can probably fix it when you provide a test case so that we can reproduce the problem.
ErikJansenIRefact commentedon Jan 8, 2019
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.
chdh commentedon Jan 8, 2019
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.
arthurschreiber commentedon Jan 8, 2019
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_ATTENTIONstate, 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! 🙇
ErikJansenIRefact commentedon Jan 8, 2019
Probably not an issue with timeouts. In the config we set "requestTimeout": 0.
But it might be caused by a cancel on the request.
chdh commentedon Jan 9, 2019
I have made some extensive tests with large records (using
varchar(max)),cancelandpause/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?
chdh commentedon Jan 10, 2019
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
tempBuilddirectory with the generated JavaScript files.testcase.zip
ErikJansenIRefact commentedon Jan 10, 2019
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.
arthurschreiber commentedon Jan 10, 2019
@ErikJansenIRefact Would you mind taking the newly released
v4.1.3for a test drive, and see if that fixes the issues you're seeing?As it's still "hidden" via the
nexttag, you can install either vianpm install tedious@nextornpm install [email protected]Please let us know if that version fixes your issues, and we'll promote it to
latest. 🙇ErikJansenIRefact commentedon Jan 10, 2019
Hi Arthur, we tested the query and the change fixes the issue.
Thanks Christian for the swift response and action.
arthurschreiber commentedon Jan 14, 2019
@ErikJansenIRefact Thanks for reporting back. I promoted
v4.1.3to thelatestchannel.@chdh Again, many thanks for looking into this! 🙇