|  | 
| 27 | 27 | import static org.openqa.selenium.remote.RemoteTags.CAPABILITIES_EVENT; | 
| 28 | 28 | import static org.openqa.selenium.remote.RemoteTags.SESSION_ID; | 
| 29 | 29 | import static org.openqa.selenium.remote.RemoteTags.SESSION_ID_EVENT; | 
|  | 30 | +import static org.openqa.selenium.remote.http.HttpMethod.DELETE; | 
| 30 | 31 | import static org.openqa.selenium.remote.tracing.AttributeKey.SESSION_URI; | 
| 31 | 32 | import static org.openqa.selenium.remote.tracing.Tags.EXCEPTION; | 
| 32 | 33 | 
 | 
|  | 
| 79 | 80 | import org.openqa.selenium.grid.data.NodeStatus; | 
| 80 | 81 | import org.openqa.selenium.grid.data.NodeStatusEvent; | 
| 81 | 82 | import org.openqa.selenium.grid.data.RequestId; | 
|  | 83 | +import org.openqa.selenium.grid.data.Session; | 
| 82 | 84 | import org.openqa.selenium.grid.data.SessionRequest; | 
| 83 | 85 | import org.openqa.selenium.grid.data.SessionRequestCapability; | 
| 84 | 86 | import org.openqa.selenium.grid.data.Slot; | 
|  | 
| 109 | 111 | import org.openqa.selenium.internal.Require; | 
| 110 | 112 | import org.openqa.selenium.remote.SessionId; | 
| 111 | 113 | import org.openqa.selenium.remote.http.HttpClient; | 
|  | 114 | +import org.openqa.selenium.remote.http.HttpRequest; | 
| 112 | 115 | import org.openqa.selenium.remote.tracing.AttributeKey; | 
| 113 | 116 | import org.openqa.selenium.remote.tracing.AttributeMap; | 
| 114 | 117 | import org.openqa.selenium.remote.tracing.Span; | 
| @@ -858,13 +861,29 @@ private void handleNewSessionRequest(SessionRequest sessionRequest) { | 
| 858 | 861 |         if (!isSessionValid && response.isRight()) { | 
| 859 | 862 |           LOG.log( | 
| 860 | 863 |               Level.INFO, | 
| 861 |  | -              "Session for request {0} has been created but it has timed out, stopping it to avoid" | 
| 862 |  | -                  + " stalled browser", | 
|  | 864 | +              "Session for request {0} has been created but it has timed out or the connection" | 
|  | 865 | +                  + " dropped, stopping it to avoid stalled browser", | 
| 863 | 866 |               reqId.toString()); | 
| 864 |  | -          URI nodeURI = response.right().getSession().getUri(); | 
| 865 |  | -          Node node = getNodeFromURI(nodeURI); | 
|  | 867 | +          Session session = response.right().getSession(); | 
|  | 868 | +          Node node = getNodeFromURI(session.getUri()); | 
| 866 | 869 |           if (node != null) { | 
| 867 |  | -            node.stop(response.right().getSession().getId()); | 
|  | 870 | +            boolean deleted; | 
|  | 871 | +            try { | 
|  | 872 | +              // Attempt to stop the session | 
|  | 873 | +              deleted = | 
|  | 874 | +                  node.execute(new HttpRequest(DELETE, "/session/" + session.getId())).getStatus() | 
|  | 875 | +                      == 200; | 
|  | 876 | +            } catch (Exception e) { | 
|  | 877 | +              LOG.log( | 
|  | 878 | +                  Level.WARNING, | 
|  | 879 | +                  String.format("Exception while trying to delete session %s", session.getId()), | 
|  | 880 | +                  e); | 
|  | 881 | +              deleted = false; | 
|  | 882 | +            } | 
|  | 883 | +            if (!deleted) { | 
|  | 884 | +              // Kill the session | 
|  | 885 | +              node.stop(session.getId()); | 
|  | 886 | +            } | 
| 868 | 887 |           } | 
| 869 | 888 |         } | 
| 870 | 889 |       } | 
|  | 
0 commit comments