Skip to content

fix(libcanhub): poll(2) timeout semantics and connect failure detail - #163

Merged
jamofer merged 3 commits into
mainfrom
fix/api3-api4-timeouts-and-last-error
Jul 27, 2026
Merged

fix(libcanhub): poll(2) timeout semantics and connect failure detail#163
jamofer merged 3 commits into
mainfrom
fix/api3-api4-timeouts-and-last-error

Conversation

@jamofer

@jamofer jamofer commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

What

Give every timeout_ms the same poll(2) meaning, and make canhub_last_error(NULL) explain why canhub_connect failed instead of crashing.

Why

Audit findings API-3 and API-4, both HIGH.

effectiveTimeout turned timeout_ms <= 0 into 5000 ms for connect/list/open, so -1 meant "block forever" in canhub_recv and "wait 5 s" everywhere else — while doc/libcanhub.md promised the former and the shipped example passed -1 expecting it. 0 meant "5 s default" in list/open but "non-blocking" in recv.

canhub_last_error dereferenced the session with no NULL check. canhub_connect returns NULL for eight distinct failures and leaves no session to ask, so the natural canhub_last_error(NULL) segfaulted on the one failure it most needed to explain.

How

-1 blocks, 0 returns immediately, >0 is a deadline. effectiveTimeout is gone and the three loops carry the same timeout_ms >= 0 guard canhub_recv already had. A thread-local buffer records why the last connect failed; every failure exit sets it and canhub_last_error(NULL) returns it. Mirrored in the Windows port, which has two extra exits (no unix scheme, winsock init). The Python backend now appends the detail to CanInitializationError, and the example prints it.

Behaviour change: callers passing 0 to canhub_list/canhub_open used to get 5 s and now get a non-blocking call. The doc never promised the old behaviour.

Testing

  • make test green (402), pytest green (14, one new)
  • e2e libcanhub.robot 6/6 in the bench container, two new cases asserting the connect-failure text
  • make windows cross-build green (_Thread_local compiles under mingw)
  • Smoke program against the built .so: struct_size mismatch, NULL config and bad url now each report their own reason; -1 still blocking after 12 s (was 5 s); 0 returns in 0.00 s
  • Same program against main segfaults on canhub_last_error(NULL)

Notes

No ABI change: same symbols, same struct layout. include/canhub.h stays comment-free — the contract lives in doc/libcanhub.md, as before.

@jamofer
jamofer merged commit 57c05d1 into main Jul 27, 2026
6 checks passed
@jamofer
jamofer deleted the fix/api3-api4-timeouts-and-last-error branch July 27, 2026 22:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant