@@ -75,12 +75,6 @@ internal JvmBridge(int portNumber)
7575
7676 private ISocketWrapper GetConnection ( )
7777 {
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 ( ) ;
8478 if ( ! _sockets . TryDequeue ( out ISocketWrapper socket ) )
8579 {
8680 socket = SocketFactory . CreateSocket ( ) ;
@@ -188,8 +182,16 @@ private object CallJavaMethod(
188182 {
189183 object returnValue = null ;
190184 ISocketWrapper socket = null ;
185+
191186 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+
193195 // dotnet-interactive does not have a dedicated thread to process
194196 // code submissions and each code submission can be processed in different
195197 // threads. DotnetHandler uses the CLR thread id to ensure that the same
@@ -290,7 +292,10 @@ private object CallJavaMethod(
290292 {
291293 // DotnetBackendHandler caught JVM exception and passed back to dotnet.
292294 // We can reuse this connection.
293- _sockets . Enqueue ( socket ) ;
295+ if ( socket != null ) // Safety check
296+ {
297+ _sockets . Enqueue ( socket ) ;
298+ }
294299 }
295300 else
296301 {
0 commit comments