Repository navigation
Csharp iox2 12 ffi testing - #20
Merged
dkroenke merged 23 commits intoMay 27, 2026
Merged
Conversation
Introduces an internal helper that pins managed state against GC moves, returns an IntPtr token for the FFI `context` parameter, and recovers the state in native callbacks. Foundation for removing the static callback state that races in Node.List() and WaitSet callbacks.
The C# constant was out of sync with the cbindgen-generated header (iceoryx2.h line 69: `#define IOX2_SERVICE_ID_LENGTH 64`). Node.List()'s manual marshaling was reading 32 bytes of service ID and then misreading the name field at offset 32 instead of 64, returning truncated / cross-contaminated strings. Discovered while fixing the static-field race in Node.List(). The existing ServiceDiscoveryTests avoided the name field (the "service names might be stored in a different format" note) which is why the pre-existing bug wasn't flagged sooner.
Removes the static `_tempServiceList` field that two concurrent callers could clobber. Each List() invocation now pins its own result list via CallbackContext and passes the IntPtr token through the native `context` parameter; the static callback recovers it via Peek. The concurrent tests added in the previous commit now pass reliably.
…tiveCallback Converts the third WaitSet callback site to CallbackContext using a small record (Callback, DisposeAttachments) as the pinned state. Drops the instance `_nativeCallback` field: all three sites now use static trampolines held as `s_*Trampoline` static readonly fields.
dkroenke
self-requested a review
April 24, 2026 11:11
Contributor
Author
|
@dkroenke do you have time to review this one? |
Contributor
dkroenke
reviewed
May 19, 2026
Files added in this PR were authored in 2026; reviewer asked the headers be dated accordingly. Existing repo files keep 2025 since that reflects their creation year.
Reviewer asked for better readability. The merged paragraph hid the three distinct fixes (Node.List race, WaitSet cross-call overwrite, SERVICE_ID_LENGTH header mismatch); separate bullets each linking to the same issue make the scope of eclipse-iceoryx#12 easier to scan.
The cleanup commit (bf33601) dropped the inline comments that explained why ServiceListCallback walks the iox2_static_config_t payload by hand (union avoidance) and what each Marshal.Copy offset corresponds to. Reviewer flagged the loss; restoring them so future readers do not have to reverse-engineer the offset arithmetic.
Reviewer asked why the silent catch dropped the previous diagnostic output. The WaitSet trampolines stay silent because they wrap arbitrary user callbacks at the FFI boundary, but ServiceListCallback runs only our own marshalling code — if it throws, that is a binding bug and the stack trace is exactly what a maintainer needs. Routing through stderr keeps it out of the normal stdout-driven console logger contract.
…uplicated Reviewer suggested sharing the storage-size literals between Iox2NativeMethods.cs and this test file. Doing so would erase the tripwire: Marshal.SizeOf<T> reads the StructLayout(Size = ...) attribute, so if both sides derive from the same const, the assert collapses to const-equals-const and trivially passes. The double-entry bookkeeping is the point — a maintainer who updates one side but forgets the other gets a failing test. Adding an in-line note so the design intent is visible at the literals, not buried in the class docstring.
dkroenke
reviewed
May 26, 2026
…contract Reviewer asked whether GCHandle has C/C++-style double-free UB and requested tests on the preliminary checks. Investigation: Probe shows that after Free, GCHandle.FromIntPtr on the original token returns a struct with IsAllocated=true (the runtime recycles handle slots, and the token resolves to whatever pin now owns that slot). Calling Free on the stale token then silently frees the new owner's pin. The `if (!handle.IsAllocated)` check inside Unpin only defends against IntPtr.Zero in practice. Document the actual safety contract (token-as-raw-pointer; double-Unpin and Peek-after-Unpin are UB) and add tests covering the boundaries we do defend: - Pin(null) -> ArgumentNullException - Peek<WrongType>(token) -> null (original pin still intact) - Unpin<WrongType>(token) -> null but GCHandle still Freed (no leak) The leak-free type-mismatch test uses the same WeakReference tripwire as the existing 10k Pin/Unpin loop.
…n codes Reviewer noticed FfiAssert was unused. Wire it into the two RuntimeTests sites that previously asserted IOX2_OK via Assert.Equal, so the helper has a concrete caller and failures carry the operation name. The newer tests under tests/Ffi/ go through Iox2.Run() / .IsOk rather than raw FFI codes, so they don't need it.
dkroenke
approved these changes
May 27, 2026
This was referenced May 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Notes for Reviewer
Iceoryx2.Native.CallbackContext(GCHandle-pinned token) and routes three FFI callback sites (Node.List,WaitSet.WaitAndProcessOnce,WaitSet.WaitAndProcessOnce(timeout),WaitSet.WaitAndProcessInternal) through it.Node.List()used a static_tempServiceListfield — two concurrent callers on the same process could share a list. Regression test:ServiceDiscoveryConcurrentTests.WaitSet._nativeCallbackwas a single instance field rewritten on everyWaitAndProcessOnce*/WaitAndProcessInternalcall — a second overlapping call would overwrite the first's closure. Now replaced by twostatic readonlytrampolines + per-call pinned context.IOX2_SERVICE_ID_LENGTHwas 32 but the cbindgen header defines it as 64 (iceoryx2.hline 69).Node.List()'s manual marshal was reading thenamefield at the wrong offset. No existing test caught this becauseServiceDiscoveryTestsdeliberately avoided thenamefield.CallbackContext.Pin/Unpinpairing in every caller (Pinoutsidetry,Unpininfinally).catch { return STOP; }— intentionally silent; swallowing user-callback exceptions is required at the FFI boundary. Pinned in tests (WaitSet_TrampolineReturnsStop_WhenCallbackThrows).NativeStructSizesTestshardcodes 9104 / 18696 with header references — treat as a tripwire, update in lockstep with cbindgen output.Pre-Review Checklist for the PR Author
Convert to draft)csharp-iox2-123-short-description)[#123] Add feature description)PR Reviewer Reminders
dotnet formathas been exectued before submittingReferences
Closes #12