Skip to content
This repository has been archived by the owner on Apr 8, 2020. It is now read-only.

NodeServices: Can't pipe result stream from chained success callback #298

Closed
ajtowf opened this issue Sep 7, 2016 · 3 comments
Closed

Comments

@ajtowf
Copy link

ajtowf commented Sep 7, 2016

Can't seem to pipe to result stream from success callback:

module.exports = result => {
    foo().then(resp => resp.stream.pipe(result.stream);
}

Invoking the callback immediately like below works just fine.

module.exports = result => {
    foo().pipe(result.stream);
}

Works fine locally, when deployed to azure we get TaskCancellationException.

Example project to reproduce https://github.com/ajtowf/nodeservices/
Live example with full stacktrace https://nodeservices.azurewebsites.net/

Am I missing something obvious here?

@SteveSandersonMS
Copy link
Member

First, thanks for supplying such a clear and straightforward repro project. That makes it so much easier to investigate this!

I'm not (yet, at least) inclined to think it would make a difference whether you're doing the piping from inside a callback or not. When you tested the synchronous case, were you still using jsreport to construct the stream, or did you simulate the synchronous case by just constructing some Node Buffer in memory or something?

My suspicion is that you just hit an issue with using jsreport. That NPM package is quite special because it's not just JavaScript code - when you install it, it does some node-gyp compilation stuff, plus it downloads the phantomjs binary which it later executes at runtime. When I first ran your repro project, the installation of jsreport failed with an error (because it failed to download the phantomjs binary), so I reinstalled it and it appeared to complete, but actually the installation was not completed, because at runtime it was triggering exceptions so severe that it brought down the whole Node process. The result of this looked a lot like the issue you described: I got TaskCancellationException because .NET could see that the Node process had terminated before it supplied any response. I then fixed this by deleting my node_modules dir completely and running npm install again - this time jsreport installed correctly first time, and worked correctly at runtime.

So, things you can try:

Make sure you're passing back any errors from Node to .NET

Your sample app doesn't currently do this. You have this:

jsreport.render("<h1>Hello, world!</h1>").then(resp => {
        resp.stream.pipe(result.stream);
});

Notice that there's no 'reject' handler on your promise, so if there's an error, this code will never send a response back to .NET, so .NET will eventually time out (saying "task was cancelled"). Add a 'reject' handler like this:

jsreport.render("<h1>Hello, world!</h1>").then(resp => {
        resp.stream.pipe(result.stream);
}, err => result(err));

Look at the log output to see if there are any more severe errors

If jsreport is triggering an error severe enough to bring down the whole Node process (which doesn't generally happen for normal NPM modules, but jsreport is not just JavaScript code), then even the code above won't be able to report the error to .NET (because the Node process would be dead already). Look at the console log, or wherever Azure puts that information, to see if it says this is happening.

Now, because I think this is an issue with using jsreport (or at least with deploying it to Azure), I'm closing this issue as "not due to JavaScriptServices". But if you disagree and think there's a bug in JavaScriptServices, please reopen and let me know! Also, I'd be very interested to hear if you discover what was causing the issue in this particular case, or if I can help further with diagnosing it. Thanks!

@ajtowf
Copy link
Author

ajtowf commented Sep 7, 2016

Thank you for your elaborate response, you're definitely right, just switched to html-pdf instead and it works perfectly in azure as well! Also thanks for the tip on hooking up the error handler as well.

Btw loved your talk in Sydney, can't believe nobody clapped (wtf?), my mind was blown at home, I clapped for myself, awesome work, keep it up!

@CallumVass
Copy link

Hi @ajtowf . I would be interested to know how you incorporated html-pdf into your .net core app if possible as I am having the same trouble when using jsreport. I just get a time out.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants