Skip to content

Commit 0a9b54f

Browse files
fix: use signer's own Free() instead of system libc free on Windows
On Windows, ctypes.util.find_library('c') returns msvcrt.dll, but the Go signer DLL (cross-compiled with MinGW) may use a different C runtime or statically link its own CRT. Calling msvcrt's free() on memory allocated by a different CRT's malloc() is undefined behavior (crashes, heap corruption). Fix by using the signer's own exported Free() function which is guaranteed to use the matching allocator. Requires the corresponding lighter-go change that exports the Free function. Co-Authored-By: Mihail <mlavric64@gmail.com>
1 parent 9c304f5 commit 0a9b54f

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

lighter/signer_client.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from lighter.models.resp_send_tx import RespSendTx
2121
from lighter.models.resp_send_tx_batch import RespSendTxBatch
2222
from lighter.transactions import CreateOrder, CancelOrder, Withdraw, CreateGroupedOrders
23-
from lighter.libc import free
2423

2524
CODE_OK = 200
2625

@@ -99,8 +98,10 @@ def decode_and_free(ptr: Any) -> Optional[str]:
9998
return c_str.decode('utf-8')
10099
return None
101100
finally:
102-
# Free the memory allocated by the C library
103-
free(ptr)
101+
# Free the memory using the signer's own Free function to ensure
102+
# the same C runtime that allocated the memory also frees it.
103+
# This is critical on Windows where different CRTs have separate heaps.
104+
__signer.Free(ptr)
104105

105106

106107
def __populate_shared_library_functions(signer):
@@ -174,6 +175,9 @@ def __populate_shared_library_functions(signer):
174175
signer.SignApproveIntegrator.argtypes = [ctypes.c_longlong, ctypes.c_uint32, ctypes.c_uint32, ctypes.c_uint32, ctypes.c_uint32, ctypes.c_longlong, ctypes.c_longlong, ctypes.c_int, ctypes.c_longlong]
175176
signer.SignApproveIntegrator.restype = SignedTxResponse
176177

178+
signer.Free.argtypes = [ctypes.c_void_p]
179+
signer.Free.restype = None
180+
177181

178182
def get_signer():
179183
# check if singleton exists already

0 commit comments

Comments
 (0)