From a52c8669a422ee35d6769f1130563193cb91dfab Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Tue, 14 Jul 2015 13:15:33 +0200 Subject: [PATCH] WalletFiles: use a ContextPropagatingThreadFactory instead of propagating the context explicitly. Fixes bug found by devrandom --- .../java/org/bitcoinj/wallet/WalletFiles.java | 25 ++++++------------- 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/wallet/WalletFiles.java b/core/src/main/java/org/bitcoinj/wallet/WalletFiles.java index 35e2e5d3..ddb0e2a7 100644 --- a/core/src/main/java/org/bitcoinj/wallet/WalletFiles.java +++ b/core/src/main/java/org/bitcoinj/wallet/WalletFiles.java @@ -18,18 +18,15 @@ package org.bitcoinj.wallet; import org.bitcoinj.core.*; -import org.bitcoinj.utils.Threading; -import com.google.common.util.concurrent.ThreadFactoryBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.bitcoinj.utils.*; +import org.slf4j.*; -import javax.annotation.Nonnull; -import java.io.File; -import java.io.IOException; +import javax.annotation.*; +import java.io.*; import java.util.concurrent.*; -import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.*; -import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.*; /** * A class that handles atomic and optionally delayed writing of the wallet file to disk. In future: backups too. @@ -67,15 +64,8 @@ public class WalletFiles { } public WalletFiles(final Wallet wallet, File file, long delay, TimeUnit delayTimeUnit) { - final ThreadFactoryBuilder builder = new ThreadFactoryBuilder() - .setDaemon(true) - .setNameFormat("Wallet autosave thread") - .setPriority(Thread.MIN_PRIORITY); // Avoid competing with the GUI thread. - Thread.UncaughtExceptionHandler handler = Threading.uncaughtExceptionHandler; - if (handler != null) - builder.setUncaughtExceptionHandler(handler); // An executor that starts up threads when needed and shuts them down later. - this.executor = new ScheduledThreadPoolExecutor(1, builder.build()); + this.executor = new ScheduledThreadPoolExecutor(1, new ContextPropagatingThreadFactory("Wallet autosave thread", Thread.MIN_PRIORITY)); this.executor.setKeepAliveTime(5, TimeUnit.SECONDS); this.executor.allowCoreThreadTimeOut(true); this.executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); @@ -88,7 +78,6 @@ public class WalletFiles { this.saver = new Callable() { @Override public Void call() throws Exception { - Context.propagate(wallet.getContext()); // Runs in an auto save thread. if (!savePending.getAndSet(false)) { // Some other scheduled request already beat us to it.