Skip to content

Commit d56ab71

Browse files
committed
[receiver/statsd] Clean up stale unix socket on startup
Remove any existing socket file before calling net.ListenPacket to prevent "address already in use" errors when the receiver restarts after an unclean shutdown (crash, SIGKILL). This is standard practice for unix socket servers. Fixes #44866
1 parent ba7ab81 commit d56ab71

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. receiver/filelog)
7+
component: receiver/statsd
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Clean up stale unix socket file on startup to prevent "address already in use" errors after unclean shutdown.
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [44866]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

receiver/statsdreceiver/internal/transport/uds_server.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ func NewUDSServer(transport Transport, socketPath string, socketPermissions os.F
2222
return nil, fmt.Errorf("NewUDSServer with %s: %w", transport.String(), ErrUnsupportedPacketTransport)
2323
}
2424

25+
// Remove any stale socket file from a previous instance that didn't
26+
// shut down cleanly (e.g., crash, SIGKILL). Ignore errors since the
27+
// file may not exist on first startup.
28+
_ = os.Remove(socketPath)
29+
2530
conn, err := net.ListenPacket(transport.String(), socketPath)
2631
if err != nil {
2732
return nil, fmt.Errorf("starting to listen %s socket: %w", transport.String(), err)

receiver/statsdreceiver/internal/transport/uds_server_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,32 @@ func Test_UDSServer_Close(t *testing.T) {
4040
assert.True(t, os.IsNotExist(err))
4141
}
4242

43+
func Test_NewUDSServer_CleansUpStaleSocket(t *testing.T) {
44+
socketPath := "/tmp/test_socket_stale"
45+
defer os.Remove(socketPath)
46+
47+
// Create first server
48+
server1, err := NewUDSServer("unixgram", socketPath, 0o622)
49+
require.NoError(t, err)
50+
require.NotNil(t, server1)
51+
52+
// Simulate a crash — close the connection but leave the socket file
53+
// (don't call server1.Close() which would remove the file)
54+
server1.(*udsServer).packetConn.Close()
55+
56+
// Verify socket file still exists (simulating stale socket after crash)
57+
_, err = os.Stat(socketPath)
58+
require.NoError(t, err, "socket file should still exist after simulated crash")
59+
60+
// Create second server on the same path — should succeed
61+
server2, err := NewUDSServer("unixgram", socketPath, 0o622)
62+
require.NoError(t, err, "should be able to start a new server on the same socket path")
63+
require.NotNil(t, server2)
64+
65+
err = server2.Close()
66+
assert.NoError(t, err)
67+
}
68+
4369
func Test_NewUDSServer_AppliesChmod(t *testing.T) {
4470
socketPath := "/tmp/test_socket_chmod"
4571
defer os.Remove(socketPath) // Cleanup after test

0 commit comments

Comments
 (0)