[14.x] stream: fix regression on duplex end
Backport Decide the return status of writeOrBuffer before calling stream.write which can reset state.length
Refs: #35941 Fixes: #35926 (closed)
Based off @mmomtchev 's work in #36229 , rebased on v14.x-staging
Checklist
-
make -j4 test
(UNIX), orvcbuild test
(Windows) passes -
tests and/or benchmarks are included -
commit message follows commit guidelines