@@ -139,8 +139,7 @@ public class LocalNode extends Node implements Closeable {
139139 private final List <SessionSlot > factories ;
140140 private final Cache <SessionId , SessionSlot > currentSessions ;
141141 private final Cache <SessionId , TemporaryFilesystem > uploadsTempFileSystem ;
142- private final Cache <UUID , TemporaryFilesystem > downloadsTempFileSystem ;
143- private final Cache <SessionId , UUID > sessionToDownloadsDir ;
142+ private final Cache <SessionId , TemporaryFilesystem > downloadsTempFileSystem ;
144143 private final AtomicInteger pendingSessions = new AtomicInteger ();
145144 private final AtomicInteger sessionCount = new AtomicInteger ();
146145 private final Runnable shutdown ;
@@ -198,7 +197,7 @@ protected LocalNode(
198197 : healthCheck ;
199198
200199 // Do not clear this cache automatically using a timer.
201- // It will be explicitly cleaned up, as and when "sessionToDownloadsDir " is auto cleaned.
200+ // It will be explicitly cleaned up, as and when "currentSessions " is auto cleaned.
202201 this .uploadsTempFileSystem =
203202 CacheBuilder .newBuilder ()
204203 .removalListener (
@@ -213,11 +212,11 @@ protected LocalNode(
213212 .build ();
214213
215214 // Do not clear this cache automatically using a timer.
216- // It will be explicitly cleaned up, as and when "sessionToDownloadsDir " is auto cleaned.
215+ // It will be explicitly cleaned up, as and when "currentSessions " is auto cleaned.
217216 this .downloadsTempFileSystem =
218217 CacheBuilder .newBuilder ()
219218 .removalListener (
220- (RemovalListener <UUID , TemporaryFilesystem >)
219+ (RemovalListener <SessionId , TemporaryFilesystem >)
221220 notification ->
222221 Optional .ofNullable (notification .getValue ())
223222 .ifPresent (
@@ -227,32 +226,6 @@ protected LocalNode(
227226 }))
228227 .build ();
229228
230- // Do not clear this cache automatically using a timer.
231- // It will be explicitly cleaned up, as and when "currentSessions" is auto cleaned.
232- this .sessionToDownloadsDir =
233- CacheBuilder .newBuilder ()
234- .removalListener (
235- (RemovalListener <SessionId , UUID >)
236- notification -> {
237- Optional .ofNullable (notification .getValue ())
238- .ifPresent (
239- value -> {
240- downloadsTempFileSystem .invalidate (value );
241- LOG .fine (
242- "Removing Downloads folder associated with "
243- + notification .getKey ());
244- });
245- Optional .ofNullable (notification .getKey ())
246- .ifPresent (
247- value -> {
248- uploadsTempFileSystem .invalidate (value );
249- LOG .fine (
250- "Removing Uploads folder associated with "
251- + notification .getKey ());
252- });
253- })
254- .build ();
255-
256229 this .currentSessions =
257230 CacheBuilder .newBuilder ()
258231 .expireAfterAccess (sessionTimeout )
@@ -386,15 +359,6 @@ public int getCurrentSessionCount() {
386359 return Math .toIntExact (n );
387360 }
388361
389- @ VisibleForTesting
390- public UUID getDownloadsIdForSession (SessionId id ) {
391- UUID uuid = sessionToDownloadsDir .getIfPresent (id );
392- if (uuid == null ) {
393- throw new NoSuchSessionException ("Cannot find session with id: " + id );
394- }
395- return uuid ;
396- }
397-
398362 @ ManagedAttribute (name = "MaxSessions" )
399363 public int getMaxSessionCount () {
400364 return maxSessionCount ;
@@ -508,21 +472,32 @@ public Either<WebDriverException, CreateSessionResponse> newSession(
508472 return Either .left (new RetrySessionRequestException ("Drain after session count reached." ));
509473 }
510474
511- UUID uuidForSessionDownloads = UUID .randomUUID ();
512475 Capabilities desiredCapabilities = sessionRequest .getDesiredCapabilities ();
476+ TemporaryFilesystem downloadsTfs ;
513477 if (managedDownloadsRequested (desiredCapabilities )) {
514- Capabilities enhanced = setDownloadsDirectory (uuidForSessionDownloads , desiredCapabilities );
478+ UUID uuidForSessionDownloads = UUID .randomUUID ();
479+
480+ downloadsTfs =
481+ TemporaryFilesystem .getTmpFsBasedOn (
482+ TemporaryFilesystem .getDefaultTmpFS ()
483+ .createTempDir ("uuid" , uuidForSessionDownloads .toString ()));
484+
485+ Capabilities enhanced = setDownloadsDirectory (downloadsTfs , desiredCapabilities );
515486 enhanced = desiredCapabilities .merge (enhanced );
516487 sessionRequest =
517488 new CreateSessionRequest (
518489 sessionRequest .getDownstreamDialects (), enhanced , sessionRequest .getMetadata ());
490+ } else {
491+ downloadsTfs = null ;
519492 }
520493
521494 Either <WebDriverException , ActiveSession > possibleSession = slotToUse .apply (sessionRequest );
522495
523496 if (possibleSession .isRight ()) {
524497 ActiveSession session = possibleSession .right ();
525- sessionToDownloadsDir .put (session .getId (), uuidForSessionDownloads );
498+ if (downloadsTfs != null ) {
499+ downloadsTempFileSystem .put (session .getId (), downloadsTfs );
500+ }
526501 currentSessions .put (session .getId (), slotToUse );
527502
528503 SessionId sessionId = session .getId ();
@@ -558,6 +533,10 @@ public Either<WebDriverException, CreateSessionResponse> newSession(
558533 getEncoder (session .getDownstreamDialect ()).apply (externalSession )));
559534 } else {
560535 slotToUse .release ();
536+ if (downloadsTfs != null ) {
537+ downloadsTfs .deleteTemporaryFiles ();
538+ downloadsTfs .deleteBaseDir ();
539+ }
561540 span .setAttribute (AttributeKey .ERROR .getKey (), true );
562541 span .setStatus (Status .ABORTED );
563542 span .addEvent ("Unable to create session with the driver" , attributeMap );
@@ -576,15 +555,8 @@ private boolean managedDownloadsRequested(Capabilities capabilities) {
576555 && Boolean .parseBoolean (downloadsEnabled .toString ());
577556 }
578557
579- private Capabilities setDownloadsDirectory (UUID uuid , Capabilities caps ) {
580- File tempDir ;
581- try {
582- TemporaryFilesystem tempFS = getDownloadsFilesystem (uuid );
583- // tempDir = tempFS.createTempDir("download", "file");
584- tempDir = tempFS .createTempDir ("download" , "" );
585- } catch (IOException e ) {
586- throw new UncheckedIOException (e );
587- }
558+ private Capabilities setDownloadsDirectory (TemporaryFilesystem downloadsTfs , Capabilities caps ) {
559+ File tempDir = downloadsTfs .createTempDir ("download" , "" );
588560 if (Browser .CHROME .is (caps ) || Browser .EDGE .is (caps )) {
589561 ImmutableMap <String , Serializable > map =
590562 ImmutableMap .of (
@@ -700,16 +672,8 @@ public TemporaryFilesystem getUploadsFilesystem(SessionId id) throws IOException
700672 }
701673
702674 @ Override
703- public TemporaryFilesystem getDownloadsFilesystem (UUID uuid ) throws IOException {
704- try {
705- return downloadsTempFileSystem .get (
706- uuid ,
707- () ->
708- TemporaryFilesystem .getTmpFsBasedOn (
709- TemporaryFilesystem .getDefaultTmpFS ().createTempDir ("uuid" , uuid .toString ())));
710- } catch (ExecutionException e ) {
711- throw new IOException (e );
712- }
675+ public TemporaryFilesystem getDownloadsFilesystem (SessionId sessionId ) throws IOException {
676+ return downloadsTempFileSystem .getIfPresent (sessionId );
713677 }
714678
715679 @ Override
@@ -746,11 +710,7 @@ public HttpResponse downloadFile(HttpRequest req, SessionId id) {
746710 + "[--enable-managed-downloads] and restart the node" ;
747711 throw new WebDriverException (msg );
748712 }
749- UUID uuid = sessionToDownloadsDir .getIfPresent (id );
750- if (uuid == null ) {
751- throw new NoSuchSessionException ("Cannot find session with id: " + id );
752- }
753- TemporaryFilesystem tempFS = downloadsTempFileSystem .getIfPresent (uuid );
713+ TemporaryFilesystem tempFS = downloadsTempFileSystem .getIfPresent (id );
754714 if (tempFS == null ) {
755715 String msg =
756716 "Cannot find downloads file system for session id: "
@@ -860,8 +820,11 @@ public HttpResponse uploadFile(HttpRequest req, SessionId id) {
860820 public void stop (SessionId id ) throws NoSuchSessionException {
861821 Require .nonNull ("Session ID" , id );
862822
863- if (sessionToDownloadsDir .getIfPresent (id ) != null ) {
864- sessionToDownloadsDir .invalidate (id );
823+ if (downloadsTempFileSystem .getIfPresent (id ) != null ) {
824+ downloadsTempFileSystem .invalidate (id );
825+ }
826+ if (uploadsTempFileSystem .getIfPresent (id ) != null ) {
827+ uploadsTempFileSystem .invalidate (id );
865828 }
866829
867830 SessionSlot slot = currentSessions .getIfPresent (id );
0 commit comments