4242import java .util .function .Consumer ;
4343import java .util .logging .Level ;
4444import java .util .logging .Logger ;
45+ import org .openqa .selenium .concurrent .ExecutorServices ;
4546import org .openqa .selenium .events .Event ;
4647import org .openqa .selenium .events .EventBus ;
4748import org .openqa .selenium .events .EventListener ;
@@ -61,6 +62,7 @@ class UnboundZmqEventBus implements EventBus {
6162 private static final Logger LOG = Logger .getLogger (EventBus .class .getName ());
6263 private static final Json JSON = new Json ();
6364 private final AtomicBoolean pollingStarted = new AtomicBoolean (false );
65+ private final PollingRunnable socketPolling ;
6466 private final ExecutorService socketPollingExecutor ;
6567 private final ExecutorService socketPublishingExecutor ;
6668 private final ExecutorService listenerNotificationExecutor ;
@@ -147,7 +149,8 @@ class UnboundZmqEventBus implements EventBus {
147149
148150 LOG .info ("Sockets created" );
149151
150- socketPollingExecutor .submit (new PollingRunnable (secret ));
152+ socketPolling = new PollingRunnable (secret );
153+ socketPollingExecutor .submit (socketPolling );
151154
152155 // Give ourselves up to a second to connect, using The World's Worst heuristic. If we don't
153156 // manage to connect, it's not the end of the world, as the socket we're connecting to may not
@@ -211,9 +214,11 @@ public void fire(Event event) {
211214
212215 @ Override
213216 public void close () {
214- socketPollingExecutor .shutdownNow ();
215- socketPublishingExecutor .shutdownNow ();
216- listenerNotificationExecutor .shutdownNow ();
217+ socketPolling .shutdown = true ;
218+ ExecutorServices .shutdownGracefully ("Event Bus Poller" , socketPollingExecutor );
219+ ExecutorServices .shutdownGracefully ("Event Bus Publisher" , socketPublishingExecutor );
220+ ExecutorServices .shutdownGracefully (
221+ "Event Bus Listener Notifier" , listenerNotificationExecutor );
217222 poller .close ();
218223
219224 if (sub != null ) {
@@ -226,14 +231,15 @@ public void close() {
226231
227232 private class PollingRunnable implements Runnable {
228233 private final Secret secret ;
234+ private volatile boolean shutdown ;
229235
230236 public PollingRunnable (Secret secret ) {
231237 this .secret = secret ;
232238 }
233239
234240 @ Override
235241 public void run () {
236- while (!Thread .currentThread ().isInterrupted ()) {
242+ while (!Thread .currentThread ().isInterrupted () && ! shutdown ) {
237243 try {
238244 int count = poller .poll (150 );
239245
0 commit comments