Skip to content

Commit 5dc21ae

Browse files
committed
Only set up NioPeerHandler after chain sync completes
No peer connections must be made until after `chain_sync_complete` is called when using the `ChannelManagerConstructor`, so initializing the `NioPeerHandler` before then just tempts fate and risks doing Bad Things.
1 parent e0935bf commit 5dc21ae

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

src/main/java/org/ldk/batteries/ChannelManagerConstructor.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ public static class InvalidSerializedDataException extends Exception {
5252
public final PeerManager peer_manager;
5353
/**
5454
* A NioPeerHandler which manages a background thread to handle socket events and pass them to the peer_manager.
55+
*
56+
* This is `null` until `chain_sync_completed` is called.
5557
*/
56-
public final NioPeerHandler nio_peer_handler;
58+
public NioPeerHandler nio_peer_handler = null;
5759
/**
5860
* If a `NetworkGraph` is provided to the constructor *and* a `LockableScore` is provided to
5961
* `chain_sync_completed`, this will be non-null after `chain_sync_completed` returns.
@@ -143,13 +145,6 @@ public ChannelManagerConstructor(byte[] channel_manager_serialized, byte[][] cha
143145
(int)(System.currentTimeMillis() / 1000),
144146
random_data, logger, ignoring_handler.as_CustomMessageHandler());
145147
}
146-
NioPeerHandler nio_peer_handler = null;
147-
try {
148-
nio_peer_handler = new NioPeerHandler(this.peer_manager);
149-
} catch (IOException e) {
150-
throw new IllegalStateException("We should never fail to construct nio objects unless we're on a platform that cannot run LDK.");
151-
}
152-
this.nio_peer_handler = nio_peer_handler;
153148
if (filter != null) {
154149
for (ChannelMonitor monitor : monitors) {
155150
monitor.load_outputs_to_watch(filter);
@@ -195,13 +190,6 @@ public ChannelManagerConstructor(Network network, UserConfig config, byte[] curr
195190
(int)(System.currentTimeMillis() / 1000),
196191
random_data, logger, ignoring_handler.as_CustomMessageHandler());
197192
}
198-
NioPeerHandler nio_peer_handler = null;
199-
try {
200-
nio_peer_handler = new NioPeerHandler(this.peer_manager);
201-
} catch (IOException e) {
202-
throw new IllegalStateException("We should never fail to construct nio objects unless we're on a platform that cannot run LDK.");
203-
}
204-
this.nio_peer_handler = nio_peer_handler;
205193
router_rand_bytes = keys_interface.get_secure_random_bytes();
206194
}
207195

@@ -226,6 +214,12 @@ public interface EventHandler {
226214
* EventHandler as required.
227215
*/
228216
public void chain_sync_completed(EventHandler event_handler, @Nullable MultiThreadedLockableScore scorer) {
217+
try {
218+
this.nio_peer_handler = new NioPeerHandler(this.peer_manager);
219+
} catch (IOException e) {
220+
throw new IllegalStateException("We should never fail to construct nio objects unless we're on a platform that cannot run LDK.");
221+
}
222+
229223
if (background_processor != null) { return; }
230224
for (TwoTuple_BlockHashChannelMonitorZ monitor: channel_monitors) {
231225
this.chain_monitor.as_Watch().watch_channel(monitor.get_b().get_funding_txo().get_a(), monitor.get_b());
@@ -274,7 +268,8 @@ public Result_NoneErrorZ persist_scorer(WriteableScore scorer) {
274268
* Interrupt the background thread, stopping the background handling of events.
275269
*/
276270
public void interrupt() {
277-
this.nio_peer_handler.interrupt();
271+
if (this.nio_peer_handler != null)
272+
this.nio_peer_handler.interrupt();
278273
this.background_processor.stop();
279274
}
280275
}

0 commit comments

Comments
 (0)