@@ -75,12 +75,6 @@ internal JvmBridge(int portNumber)
75
75
76
76
private ISocketWrapper GetConnection ( )
77
77
{
78
- // Limit the number of connections to the JVM backend. Netty is configured
79
- // to use a set number of threads to process incoming connections. Each
80
- // new connection is delegated to these threads in a round robin fashion.
81
- // A deadlock can occur on the JVM if a new connection is scheduled on a
82
- // blocked thread.
83
- _socketSemaphore . Wait ( ) ;
84
78
if ( ! _sockets . TryDequeue ( out ISocketWrapper socket ) )
85
79
{
86
80
socket = SocketFactory . CreateSocket ( ) ;
@@ -188,8 +182,16 @@ private object CallJavaMethod(
188
182
{
189
183
object returnValue = null ;
190
184
ISocketWrapper socket = null ;
185
+
191
186
try
192
- {
187
+ {
188
+ // Limit the number of connections to the JVM backend. Netty is configured
189
+ // to use a set number of threads to process incoming connections. Each
190
+ // new connection is delegated to these threads in a round robin fashion.
191
+ // A deadlock can occur on the JVM if a new connection is scheduled on a
192
+ // blocked thread.
193
+ _socketSemaphore . Wait ( ) ;
194
+
193
195
// dotnet-interactive does not have a dedicated thread to process
194
196
// code submissions and each code submission can be processed in different
195
197
// threads. DotnetHandler uses the CLR thread id to ensure that the same
@@ -290,7 +292,10 @@ private object CallJavaMethod(
290
292
{
291
293
// DotnetBackendHandler caught JVM exception and passed back to dotnet.
292
294
// We can reuse this connection.
293
- _sockets . Enqueue ( socket ) ;
295
+ if ( socket != null ) // Safety check
296
+ {
297
+ _sockets . Enqueue ( socket ) ;
298
+ }
294
299
}
295
300
else
296
301
{
0 commit comments