title: spring webflux - websocketclient threads not terminating
body: <p>I'm using a Spring WebFlux <code>WebSocketClient</code> to subscribe to and handle messages from a remote web socket.  During processing the Flux of messages from the remote socket will sometimes unexpectedly complete (or terminate on error) causing the web socket client's <code>onComplete</code> (or <code>onError</code>) callback to execute. When this occurs, my <code>onComplete</code> and <code>onError</code> callbacks publish an event.  An event listener responds by calling the function that creates another web socket client which connects to the same external web socket and the socket processing starts over again.</p>
  
  <p>My problem is that I cannot figure out how to free the <code>WebSocketClient</code> resources after a client completes processing.  This causes unused threads to accumulate in the JVM. In particular the threads on which the first <code>WebSocketClient</code> were running (<code>WebSocketClient-SecureIO-1</code>, <code>WebSocketClient-SecureIO-2</code> and <code>parallel-1</code>) remain in a waiting state and new threads are started for the new 'WebSocketClient'. I thought calling <code>close()</code> on <code>WebSocketSession</code> would solve the problem, but it does not.</p>
  
  <p>The pattern of my implementation is: </p>
  
  <pre><code>public void startProcessing() {
     WebSocketClient client = new StandardWebSocketClient();
     Mono&lt;String&gt; subscribeMsg = Mono.just("...");
  
     client
        .execute(uri, webSocketSession -&gt;
            webSocketSession
               .send(subscribeMsg.map(webSocketSession::textMessage))
               .thenMany(webSocketSession.receive())
               .map(webSocketMessage -&gt; ...)
               .buffer(Duration.ofSeconds(bufferDuration))
               .doOnNext(handler)
               .doOnComplete(() -&gt; webSocketSession.close())
               .then())
         .subscribe(
              aVoid -&gt; LOGGER.info("subscription started"),
              throwable -&gt; {... publish restart event ...},
              () -&gt; {... publish restart event ...});
  }
  
  public void restartEventListener() {
      startProcessing();
  }
  </code></pre>
  
  <p>Any suggestions on how I can prevent unused <code>WebSocketClient</code> threads from accumulating in the JVM?</p>