Skip to content

Commit 3977eaa

Browse files
authored
Merge pull request #166 from aKzenT/fix_165
Handle process outputs correctly. Fixes #165
2 parents 9e235ec + 678aff5 commit 3977eaa

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

Changelog.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ This project uses [semantic versioning](http://semver.org/spec/v2.0.0.html). Ref
33
*[Semantic Versioning in Practice](https://www.jering.tech/articles/semantic-versioning-in-practice)*
44
for an overview of semantic versioning.
55

6-
## [Unreleased](https://github.com/JeringTech/Javascript.NodeJS/compare/7.0.0-beta.3...HEAD)
6+
## [Unreleased](https://github.com/JeringTech/Javascript.NodeJS/compare/7.0.0-beta.4...HEAD)
7+
8+
## [7.0.0-beta.4](https://github.com/JeringTech/Javascript.NodeJS/compare/7.0.0-beta.3...7.0.0-beta.4) - Apr 18, 2023
9+
### Fixes
10+
- Handle process outputs correctly. ([#166](https://github.com/JeringTech/Javascript.NodeJS/pull/166)).
711

812
## [7.0.0-beta.3](https://github.com/JeringTech/Javascript.NodeJS/compare/7.0.0-beta.2...7.0.0-beta.3) - Feb 14, 2023
913
### Additions

src/NodeJS/NodeJSServiceImplementations/OutOfProcess/NodeJSProcess.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,14 @@ private void OutputThreadStart(object? arg)
106106
try
107107
{
108108
string? data;
109-
while ((data = streamReader.ReadLine()) != null)
109+
do
110110
{
111+
data = streamReader.ReadLine();
111112
if (TryCreateMessage(_outputDataStringBuilder, data, out string? message))
112113
{
113114
_outputReceivedHandler!(message);
114115
}
115-
}
116+
} while (data != null);
116117
}
117118
catch (IOException)
118119
{
@@ -131,13 +132,14 @@ private void ErrorThreadStart(object? arg)
131132
try
132133
{
133134
string? data;
134-
while ((data = streamReader.ReadLine()) != null)
135+
do
135136
{
137+
data = streamReader.ReadLine();
136138
if (TryCreateMessage(_errorDataStringBuilder, data, out string? message))
137139
{
138140
_errorReceivedHandler!(message);
139141
}
140-
}
142+
} while (data != null);
141143
}
142144
catch (IOException)
143145
{
@@ -387,13 +389,22 @@ protected virtual async ValueTask DisposeAsyncCore()
387389
try
388390
{
389391
_process.Kill();
390-
await _process.WaitForExitAsync().ConfigureAwait(false);
391392
}
392393
catch
393394
{
394395
// Throws if process is already dead, note that process could die between HasExited check and Kill
395396
}
396397
}
398+
399+
// Wait for exit
400+
//
401+
// Even after HasExited is true, there may still be output to be read. See https://github.com/dotnet/runtime/blob/91b93eb22bc7d9029a38469e55aa72d52c087834/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs#L1475.
402+
//
403+
// Should not throw. Process has started so Process._haveProcessID and Process._haveProcessHandle are true. They are only set to false
404+
// by Process.Close, which is only called by Process.Dispose, called below.
405+
await _process.WaitForExitAsync().ConfigureAwait(false);
406+
407+
// Dispose
397408
_process.Dispose();
398409

399410
_disposed = true;
@@ -445,13 +456,17 @@ protected virtual void Dispose(bool disposing)
445456
try
446457
{
447458
_process?.Kill();
448-
_process?.WaitForExit(); // Blocks
449459
}
450460
catch
451461
{
452462
// Throws if process is already dead, note that process could die between HasExited check and Kill
453463
}
454464
}
465+
466+
// Wait for exit (see DisposeAsync for why we call this outside of the HasExited == false block)
467+
_process?.WaitForExit(); // Blocks
468+
469+
// Dispose
455470
_process?.Dispose();
456471
}
457472

0 commit comments

Comments
 (0)