dicer icon indicating copy to clipboard operation
dicer copied to clipboard

lib: use `Writable#_final` for 'finish' tracking

Open indutny-signal opened this issue 2 years ago • 13 comments

Instead overriding emit method and using this._realFinish to detect unexpected end of data - implement _final method and track the logic there.

The only behavior change from the use of _final is that we would no longer emit finish when the input data is terminated early. Instead we would emit error as before and stop the processing. Note that this is the behavior provided by stream.Writable, and it is thus conformant with the specification.

Additionally, replace uses of private _events property with either straight emit() with return value check, or listenerCount() in the situations where there might be an overhead from the constructed event arguments.

Fix: #26

cc @mscdex (this is @indutny under disguise)

indutny-signal avatar Mar 02 '22 01:03 indutny-signal

I didn't run the formidable benchmarks, but the ones in the repo appears to be giving roughly the same timing as before the patch:

indutny@Fedors-MacBook-Pro-2 dicer % git co master 
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
indutny@Fedors-MacBook-Pro-2 dicer % node bench/bench.js dicer
dicer: 419.234ms
indutny@Fedors-MacBook-Pro-2 dicer % node bench/bench.js dicer
dicer: 432.782ms
indutny@Fedors-MacBook-Pro-2 dicer % git co fix/pipeline-incompat 
Switched to branch 'fix/pipeline-incompat'
Your branch is up to date with 'sig/fix/pipeline-incompat'.
indutny@Fedors-MacBook-Pro-2 dicer % node bench/bench.js dicer    
dicer: 418.913ms
indutny@Fedors-MacBook-Pro-2 dicer % node bench/bench.js dicer
dicer: 423.359ms

indutny-signal avatar Mar 02 '22 01:03 indutny-signal

FWIW, I've tested it on our codebase (Signal-Desktop) and the pipeline() test pass there as well.

indutny-signal avatar Mar 02 '22 01:03 indutny-signal

Welp, sorry for CI failures. Force pushed.

indutny-signal avatar Mar 02 '22 01:03 indutny-signal

Oh, it is actually failing on older node.js versions... Weird.

indutny-signal avatar Mar 02 '22 01:03 indutny-signal

Ah, stream/promises.

indutny-signal avatar Mar 02 '22 01:03 indutny-signal

Oof, this was tricky. Node 10 and 12 have different event behavior from 14 and 16. In particular, I get both error and end events on part and dicer on <= 12, and only 'error' on > 12.

Additionally, I had to delay the cb() in _final because it wasn't giving the part a chance to fire its error on Node <= 12.

Weird stuff!

indutny-signal avatar Mar 02 '22 02:03 indutny-signal

Anyway, it is all fixed now, although I'm much less happy with the PR than I was at the start. (Hacks :sob:)

indutny-signal avatar Mar 02 '22 02:03 indutny-signal

Yay, all green! :grin:

indutny avatar Mar 02 '22 03:03 indutny

Totally no rush with this. Just let me know if you'll ever need any additional information or more detailed description of the changes to help you review this. Thanks!

indutny-signal avatar Mar 03 '22 01:03 indutny-signal

(Friendly weekly reminder so that this PR doesn't fall through the cracks of your inbox)

indutny-signal avatar Mar 09 '22 20:03 indutny-signal

(Friendly reminder after one month :wink:)

indutny-signal avatar Apr 04 '22 20:04 indutny-signal

(This PR is still open :grin: )

indutny-signal avatar Apr 25 '22 16:04 indutny-signal

I ran into this issue today, using pipeline from stream/promises. Wondering why this PR hasn't been merged 😅

JoshuaWise avatar Jul 21 '23 10:07 JoshuaWise