Skip to content

Commit 76d0eaa

Browse files
committed
Unset background processor and dangle it upon stopping.
1 parent f4bb3a1 commit 76d0eaa

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

bindings/batteries/ChannelManagerConstructor.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,12 @@ public class ChannelManagerConstructor: NativeTypeWrapper {
177177
}
178178

179179
public func interrupt() {
180+
print("stopping background processor")
180181
self.shutdown = true
181-
self.backgroundProcessor?.stop()
182+
self.backgroundProcessor?.dangle().stop()
183+
print("stopped background processor")
184+
self.backgroundProcessor = nil
185+
print("unset background processor")
182186
}
183187

184188

ci/LDKSwift/Tests/LDKSwiftTests/LDKSwiftTest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ class LDKSwiftTest: XCTestCase {
7676
channel_manager_constructor.chain_sync_completed(persister: cmPersister)
7777
channel_manager_constructor.interrupt()
7878

79-
// Bindings.removeInstancePointer(instance: cmPersister)
80-
79+
print("final cleanup")
80+
Bindings.removeInstancePointer(instance: cmPersister)
8181
}
8282

8383
/*

templates/BindingsTemplate.swift

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ open class NativeTypeWrapper: Hashable {
2020
internal let globalInstanceNumber: UInt
2121
internal var dangling = false
2222
internal private(set) var anchors: Set<NativeTypeWrapper> = []
23+
internal var pointerDebugDescription: String? = nil
2324

2425
init(conflictAvoidingVariableName: UInt) {
2526
Self.globalInstanceCounter += 1
@@ -182,25 +183,42 @@ public class Bindings{
182183
}
183184
/* STATIC_METHODS_END */
184185

185-
static var nativelyExposedInstances = [String: AnyObject]()
186+
static var nativelyExposedInstances = [String: NativeTypeWrapper]()
186187

187-
public class func instanceToPointer(instance: AnyObject) -> UnsafeMutableRawPointer {
188+
public class func instanceToPointer(instance: NativeTypeWrapper) -> UnsafeMutableRawPointer {
188189
let pointer = Unmanaged.passUnretained(instance).toOpaque()
190+
instance.pointerDebugDescription = pointer.debugDescription
189191
Self.nativelyExposedInstances[pointer.debugDescription] = instance
190192
return pointer
191193
}
192194

193-
public class func pointerToInstance<T: AnyObject>(pointer: UnsafeRawPointer, sourceMarker: String?) -> T{
195+
public class func pointerToInstance<T: NativeTypeWrapper>(pointer: UnsafeRawPointer, sourceMarker: String?) -> T{
194196

195197
let callStack = Thread.callStackSymbols
196198
let caller = sourceMarker ?? callStack[1]
197-
print("Retrieving instance from pointer for caller: \(caller)")
199+
// print("Retrieving instance from pointer for caller: \(caller)")
198200
// let value = Unmanaged<T>.fromOpaque(pointer).takeUnretainedValue()
199201
let value = Self.nativelyExposedInstances[pointer.debugDescription] as! T
200-
print("Instance retrieved for caller: \(caller)")
202+
// print("Instance retrieved for caller: \(caller)")
201203
return value
202204
}
203205

206+
public class func removeInstancePointer(instance: NativeTypeWrapper) -> Bool {
207+
guard let debugDescription = instance.pointerDebugDescription else {
208+
return false
209+
}
210+
Self.nativelyExposedInstances.removeValue(forKey: debugDescription)
211+
instance.pointerDebugDescription = nil
212+
return true
213+
}
214+
215+
public class func clearInstancePointers() {
216+
for (_, currentInstance) in Self.nativelyExposedInstances {
217+
currentInstance.pointerDebugDescription = nil
218+
}
219+
Self.nativelyExposedInstances.removeAll()
220+
}
221+
204222
/* SWIFT_TO_RUST_START */
205223
public class func new_LDKTransactionWrapper(array: [UInt8]) -> LDKTransactionWrapper {
206224
/* DIMENSION_REDUCTION_PREP */
@@ -332,11 +350,11 @@ public class Bindings{
332350

333351
}
334352

335-
public class InstanceCrashSimulator {
353+
public class InstanceCrashSimulator: NativeTypeWrapper {
336354

337355
public init() {
338-
339-
}
356+
super.init(conflictAvoidingVariableName: 0)
357+
}
340358

341359
public func getPointer() -> UnsafeMutableRawPointer {
342360
let pointer = Bindings.instanceToPointer(instance: self)

0 commit comments

Comments
 (0)