Skip to content

zitilib: ZIti_connect honor socket non/blocking status#1016

Merged
ekoby merged 6 commits intomainfrom
zitilib-connect-non-block
Mar 25, 2026
Merged

zitilib: ZIti_connect honor socket non/blocking status#1016
ekoby merged 6 commits intomainfrom
zitilib-connect-non-block

Conversation

@ekoby
Copy link
Copy Markdown
Member

@ekoby ekoby commented Mar 24, 2026

[fixes #984]

  1. Non-Blocking Ziti_connect and Ziti_connect_addr.
    The core zitilib connection functions have been redesigned to support non-blocking operations:

    • Asynchronous Initialization: Instead of blocking until a Ziti connection is fully established, these functions now initiate the process in a background worker thread.
    • Ephemeral Bridge: They create a local ephemeral "bridge" socket and immediately return the result of a standard connect() to this bridge.
    • Standard Multiplexing: If the application socket is non-blocking, Ziti_connect will return EINPROGRESS (or WSAEWOULDBLOCK on Windows), allowing applications to use select, poll, or epoll to monitor connection
      progress.
  2. IPv6 Support

    • The internal bridge mechanism now supports both IPv4 and IPv6 loopback addresses.
    • New utility function zl_socket_af identifies the address family of a given socket to ensure the correct bridge type is created.
  3. Cross-Platform Consistency & Stability

    • Background Bridging: Connection bridging now utilizes uv_queue_work to prevent the Ziti event loop from being blocked by synchronous socket operations.
    • API Refinement: ziti_conn_bridge_fds now uses uv_os_sock_t for better type safety across platforms.
  4. Sample Program Updates

  • ziti-fd-client: Extensively updated to demonstrate the new non-blocking API, including manual poll() usage and an optional blocking mode via the ZITI_SAMPLE_BLOCKING environment variable.
  • ziti-ncat: Updated to align with API changes and ensure stable connection handling.

ekoby and others added 2 commits March 24, 2026 14:49
Refactor the zitilib connect path to support non-blocking sockets:
return EINPROGRESS and let callers poll/select for completion. The app
fd is dup'd so the caller can close it before connect completes.

Replace the Unix socketpair/dup2 bridge mechanism with loopback
listen/accept (matching the Windows approach) and add AF_INET6 support
to both platform implementations.

Also fix ziti_conn_bridge_fds parameter types (uv_os_fd_t -> uv_os_sock_t)
and correct the inverted error check in ziti-fd-client sample.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@ekoby ekoby requested a review from a team as a code owner March 24, 2026 19:25
@ekoby ekoby added the minor bump minor version label Mar 24, 2026

#if _WIN32
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be done in ziti_lib_init on windows? or should ziti_lib track that it's been called somehow? it seems important to do (since you're doing it here). how would python or any other FFI handle this? does it need to?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a sample app and it is opening socket before Ziti_lib_init()

Copy link
Copy Markdown
Member

@dovholuknf dovholuknf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one leak question wrt req->conn but other than that lgtm

@ekoby ekoby merged commit a1fbe6b into main Mar 25, 2026
19 of 20 checks passed
@ekoby ekoby deleted the zitilib-connect-non-block branch March 25, 2026 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

minor bump minor version

Projects

None yet

Development

Successfully merging this pull request may close these issues.

zitilib: Ziti_connect() and Ziti_connect_addr() should support non-blocking operation

2 participants