Repository navigation
Conversation
Add an address-caching listener that snapshots each accepted connection's local and remote addresses before handing it to Caddy. This keeps address information available after the underlying userspace network endpoint is closed. If a connection already has a nil local or remote address when accepted, close it and continue accepting rather than passing an unusable connection to Caddy or fabricating metadata. This avoids invalid addresses reaching logging, matching, or authentication. Apply the wrapper to both shared TCP listeners and TLS listeners so the behavior is consistent across both listener paths. Fixes mholt/caddy-l4#490 Signed-off-by: wodev <14372617+wodev@users.noreply.github.com>
Add focused tests for the address-caching listener used by the TCP and TLS listener paths. Verify that accepted connections retain their local and remote addresses after the underlying connection closes, so downstream code can still access valid address metadata. Cover connections with a missing local address, remote address, or both, and confirm the listener closes and skips them in favor of the next valid connection. Also verify that accept errors are propagated unchanged, including when they occur after a connection with missing addresses has been rejected. Exercise the TLS listener ordering used by getTLSListener and confirm that the accepted TLS connection continues to expose the cached addresses when its underlying endpoint closes. Related to mholt/caddy-l4#490 Signed-off-by: wodev <14372617+wodev@users.noreply.github.com>
wodev
force-pushed
the
fix-caddy-l4-proxy-nil-exception
branch
from
November 2, 2026 22:36
45a89ae to
8592ea9
Compare
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.
Summary
Prevent nil connection addresses from reaching Caddy by caching each accepted tsnet connection’s local and remote addresses at the listener boundary.
This PR adds an address-caching listener to both the TCP and TLS listener paths, together with focused regression tests. Connections whose addresses are already unavailable when accepted are closed and skipped rather than passed to Caddy with invalid metadata.
Problem
Running
caddy-l4https://github.com/mholt/caddy-l4 with acaddy-tailscalelistener can cause a sporadic nil-pointer panic that terminates the entire Caddy process. The reported failure occurs inlayer4.(*Server).handle, where connection address information is accessed for logging. Updating the Tailscale dependency tov1.104.4did not resolve the reported crash.The underlying userspace TCP connection can return
nilfromRemoteAddr()when its network endpoint is no longer connected. Downstream code that subsequently calls:can therefore panic. Address availability depends on endpoint state, so the failure is timing-sensitive: a connection may initially have valid addresses but lose them before downstream handling or logging completes.
The original proposal added address caching to
caddy-l4. During review, a maintainer recommended addressing the behavior outsidecaddy-l4rather than adding dependency-specific complexity there. This PR moves the compatibility handling to the point wherecaddy-tailscaledelivers connections to Caddy.Changes
Cache addresses before handing connections to Caddy
The new
addressCachingListenerwraps the underlying listener. For each successfulAccept(), it reads the connection’sLocalAddr()andRemoteAddr()and returns anaddressCachingConncontaining those values.The connection wrapper overrides only the two address methods:
Subsequent address lookups return the captured values without consulting the underlying endpoint. The standard
net.Connoperations—including reads, writes, deadlines, and close—remain delegated to the embedded connection.This keeps address metadata available after the underlying endpoint closes, allowing downstream logging and address-based processing to continue safely.
Reject connections whose addresses are already unavailable
Successful acceptance does not guarantee that the peer is still connected when its addresses are inspected. A peer may reset the connection before the listener can capture its metadata.
If either address is
nil, the wrapper closes the connection and continues accepting. It deliberately does not substitute an empty or synthetic address, because fabricated metadata could affect logging, matching, or authentication. Errors returned by the underlying listener are propagated unchanged.Apply the wrapper to both TCP and TLS listeners
For plain TCP, the shared listener wraps the tsnet listener directly.
For TLS, address caching is installed below
tls.NewListener:This captures addresses before TLS handling while preserving the outer
*tls.Connexposed to Caddy. Both listener paths receive the same address-preservation behavior.Regression tests
The added tests cover:
Network()andString()remain usable after the underlying connection closes.*tls.Connvalues and continue exposing cached addresses after their underlying endpoint closes.These are deterministic unit tests using test doubles; they do not require a running Tailscale node. The TLS test verifies the intended wrapper ordering without performing a handshake.
Scope and limitations
This change is limited to accepted connections on the TCP and TLS listener paths. It does not modify UDP handling, outbound dialing, or
caddy-l4.The wrapper retains the returned
net.Addrvalues; it does not deep-copy arbitrary address implementations. It addresses the reported failure mode in which later address lookups return nil.The tests validate the wrapper’s behavior, but do not reproduce the complete Caddy/tsnet deployment or invoke the configured listener factories directly. End-to-end testing with repeated connection resets remains useful for confirming the reported production failure is resolved.
Related issue
Fixes mholt/caddy-l4#490.
Co-Authored by GPT-6.1 Sol