@@ -57,7 +57,7 @@ public SocketNodeInstance(string projectPath, string[] watchFileExtensions, stri
5757 _socketAddress = socketAddress ;
5858 }
5959
60- protected override async Task < T > InvokeExportAsync < T > ( NodeInvocationInfo invocationInfo )
60+ protected override async Task < T > InvokeExportAsync < T > ( NodeInvocationInfo invocationInfo , CancellationToken cancellationToken )
6161 {
6262 if ( _connectionHasFailed )
6363 {
@@ -70,7 +70,12 @@ protected override async Task<T> InvokeExportAsync<T>(NodeInvocationInfo invocat
7070
7171 if ( _virtualConnectionClient == null )
7272 {
73- await EnsureVirtualConnectionClientCreated ( ) ;
73+ // Although we could pass the cancellationToken into EnsureVirtualConnectionClientCreated and
74+ // have it signal cancellations upstream, that would be a bad thing to do, because all callers
75+ // wait for the same connection task. There's no reason why the first caller should have the
76+ // special ability to cancel the connection process in a way that would affect subsequent
77+ // callers. So, each caller just independently stops awaiting connection if that call is cancelled.
78+ await EnsureVirtualConnectionClientCreated ( ) . OrThrowOnCancellation ( cancellationToken ) ;
7479 }
7580
7681 // For each invocation, we open a new virtual connection. This gives an API equivalent to opening a new
@@ -83,7 +88,7 @@ protected override async Task<T> InvokeExportAsync<T>(NodeInvocationInfo invocat
8388 virtualConnection = _virtualConnectionClient . OpenVirtualConnection ( ) ;
8489
8590 // Send request
86- await WriteJsonLineAsync ( virtualConnection , invocationInfo ) ;
91+ await WriteJsonLineAsync ( virtualConnection , invocationInfo , cancellationToken ) ;
8792
8893 // Determine what kind of response format is expected
8994 if ( typeof ( T ) == typeof ( Stream ) )
@@ -96,7 +101,7 @@ protected override async Task<T> InvokeExportAsync<T>(NodeInvocationInfo invocat
96101 else
97102 {
98103 // Parse and return non-streamed JSON response
99- var response = await ReadJsonAsync < RpcJsonResponse < T > > ( virtualConnection ) ;
104+ var response = await ReadJsonAsync < RpcJsonResponse < T > > ( virtualConnection , cancellationToken ) ;
100105 if ( response . ErrorMessage != null )
101106 {
102107 throw new NodeInvocationException ( response . ErrorMessage , response . ErrorDetails ) ;
@@ -163,27 +168,27 @@ protected override void Dispose(bool disposing)
163168 base . Dispose ( disposing ) ;
164169 }
165170
166- private static async Task WriteJsonLineAsync ( Stream stream , object serializableObject )
171+ private static async Task WriteJsonLineAsync ( Stream stream , object serializableObject , CancellationToken cancellationToken )
167172 {
168173 var json = JsonConvert . SerializeObject ( serializableObject , jsonSerializerSettings ) ;
169174 var bytes = Encoding . UTF8 . GetBytes ( json + '\n ' ) ;
170- await stream . WriteAsync ( bytes , 0 , bytes . Length ) ;
175+ await stream . WriteAsync ( bytes , 0 , bytes . Length , cancellationToken ) ;
171176 }
172177
173- private static async Task < T > ReadJsonAsync < T > ( Stream stream )
178+ private static async Task < T > ReadJsonAsync < T > ( Stream stream , CancellationToken cancellationToken )
174179 {
175- var json = Encoding . UTF8 . GetString ( await ReadAllBytesAsync ( stream ) ) ;
180+ var json = Encoding . UTF8 . GetString ( await ReadAllBytesAsync ( stream , cancellationToken ) ) ;
176181 return JsonConvert . DeserializeObject < T > ( json , jsonSerializerSettings ) ;
177182 }
178183
179- private static async Task < byte [ ] > ReadAllBytesAsync ( Stream input )
184+ private static async Task < byte [ ] > ReadAllBytesAsync ( Stream input , CancellationToken cancellationToken )
180185 {
181186 byte [ ] buffer = new byte [ 16 * 1024 ] ;
182187
183188 using ( var ms = new MemoryStream ( ) )
184189 {
185190 int read ;
186- while ( ( read = await input . ReadAsync ( buffer , 0 , buffer . Length ) ) > 0 )
191+ while ( ( read = await input . ReadAsync ( buffer , 0 , buffer . Length , cancellationToken ) ) > 0 )
187192 {
188193 ms . Write ( buffer , 0 , read ) ;
189194 }
0 commit comments