44
55import datadog .trace .api .config .ProfilingConfig ;
66import datadog .trace .bootstrap .config .provider .ConfigProvider ;
7+ import datadog .trace .util .AgentTaskScheduler ;
78import datadog .trace .util .PidHelper ;
89import java .io .IOException ;
910import java .nio .file .FileVisitResult ;
1718import java .time .Instant ;
1819import java .time .temporal .ChronoUnit ;
1920import java .util .Set ;
20- import java .util .concurrent .CompletableFuture ;
21- import java .util .concurrent .ExecutionException ;
21+ import java .util .concurrent .CountDownLatch ;
2222import java .util .concurrent .TimeUnit ;
23- import java .util .concurrent .TimeoutException ;
2423import org .slf4j .Logger ;
2524import org .slf4j .LoggerFactory ;
2625
@@ -64,7 +63,7 @@ default FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOE
6463 default void onCleanupStart (boolean selfCleanup , long timeout , TimeUnit unit ) {}
6564 }
6665
67- private class CleanupVisitor implements FileVisitor <Path > {
66+ private final class CleanupVisitor implements FileVisitor <Path > {
6867 private boolean shouldClean ;
6968
7069 private final Set <String > pidSet = PidHelper .getJavaPids ();
@@ -175,12 +174,37 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx
175174 }
176175 }
177176
177+ private final class CleanupTask implements Runnable {
178+ private final CountDownLatch latch = new CountDownLatch (1 );
179+ private volatile Throwable throwable = null ;
180+
181+ @ Override
182+ public void run () {
183+ try {
184+ cleanup (false );
185+ } catch (OutOfMemoryError oom ) {
186+ throw oom ;
187+ } catch (Throwable t ) {
188+ throwable = t ;
189+ } finally {
190+ latch .countDown ();
191+ }
192+ }
193+
194+ boolean await (long timeout , TimeUnit unit ) throws Throwable {
195+ boolean ret = latch .await (timeout , unit );
196+ if (throwable != null ) {
197+ throw throwable ;
198+ }
199+ return ret ;
200+ }
201+ }
202+
178203 private final Path baseTempDir ;
179204 private final Path tempDir ;
180205 private final long cutoffSeconds ;
181206
182- private final CompletableFuture <Void > cleanupTask ;
183-
207+ private final CleanupTask cleanupTask = new CleanupTask ();
184208 private final CleanupHook cleanupTestHook ;
185209
186210 /**
@@ -202,11 +226,7 @@ public static TempLocationManager getInstance() {
202226 static TempLocationManager getInstance (boolean waitForCleanup ) {
203227 TempLocationManager instance = SingletonHolder .INSTANCE ;
204228 if (waitForCleanup ) {
205- try {
206- instance .waitForCleanup (5 , TimeUnit .SECONDS );
207- } catch (TimeoutException ignored ) {
208-
209- }
229+ instance .waitForCleanup (5 , TimeUnit .SECONDS );
210230 }
211231 return instance ;
212232 }
@@ -259,20 +279,17 @@ private TempLocationManager() {
259279 baseTempDir .toFile ().deleteOnExit ();
260280
261281 tempDir = baseTempDir .resolve ("pid_" + pid );
262- cleanupTask = CompletableFuture . runAsync (() -> cleanup (false ));
282+ AgentTaskScheduler . INSTANCE . execute (() -> cleanup (false ));
263283
264284 Thread selfCleanup =
265285 new Thread (
266286 () -> {
267- try {
268- waitForCleanup (1 , TimeUnit .SECONDS );
269- } catch (TimeoutException e ) {
287+ if (!waitForCleanup (1 , TimeUnit .SECONDS )) {
270288 log .info (
271289 "Cleanup task timed out. {} temp directory might not have been cleaned up properly" ,
272290 tempDir );
273- } finally {
274- cleanup (true );
275291 }
292+ cleanup (true );
276293 },
277294 "Temp Location Manager Cleanup" );
278295 Runtime .getRuntime ().addShutdownHook (selfCleanup );
@@ -362,21 +379,19 @@ boolean cleanup(boolean cleanSelf, long timeout, TimeUnit unit) {
362379 }
363380
364381 // accessible for tests
365- void waitForCleanup (long timeout , TimeUnit unit ) throws TimeoutException {
382+ boolean waitForCleanup (long timeout , TimeUnit unit ) {
366383 try {
367- cleanupTask .get (timeout , unit );
384+ return cleanupTask .await (timeout , unit );
368385 } catch (InterruptedException e ) {
369- cleanupTask . cancel ( true );
386+ log . debug ( "Temp directory cleanup was interrupted" );
370387 Thread .currentThread ().interrupt ();
371- } catch (TimeoutException e ) {
372- cleanupTask .cancel (true );
373- throw e ;
374- } catch (ExecutionException e ) {
388+ } catch (Throwable t ) {
375389 if (log .isDebugEnabled ()) {
376- log .debug ("Failed to cleanup temp directory: {}" , tempDir , e );
390+ log .debug ("Failed to cleanup temp directory: {}" , tempDir , t );
377391 } else {
378392 log .debug ("Failed to cleanup temp directory: {}" , tempDir );
379393 }
380394 }
395+ return false ;
381396 }
382397}
0 commit comments