{"record":{"id":"8baa34bf5e8989b6","repo":"temporalio/temporal","slug":"failed-assigning-ephemeral-port-w","errorCode":null,"errorMessage":"failed assigning ephemeral port: %w","messagePattern":"failed assigning ephemeral port: %w","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/testing/freeport/freeport.go","lineNumber":30,"sourceCode":"// This works by binding a new TCP socket on port 0, which requests the OS to\n// allocate a free port. There is no strict guarantee that the port will remain\n// available after this function returns, but it should be safe to assume that\n// a given port will not be allocated again to any process on this machine\n// within a few seconds.\n//\n// On Unix-based systems, binding to the port returned by this function requires\n// setting the `SO_REUSEADDR` socket option (Go already does that by default,\n// but other languages may not); otherwise, the OS may fail with a message such\n// as \"address already in use\". Windows default behavior is already appropriate\n// in this regard; on that platform, `SO_REUSEADDR` has a different meaning and\n// should not be set (setting it may have unpredictable consequences).\nfunc MustGetFreePort() int {\n\tport, err := getFreePort(\"127.0.0.1\")\n\tif err != nil {\n\t\t// try ipv6\n\t\tport, err = getFreePort(\"[::1]\")\n\t\tif err != nil {\n\t\t\tpanic(fmt.Errorf(\"failed assigning ephemeral port: %w\", err))\n\t\t}\n\t}\n\treturn port\n}\n\nfunc getFreePort(host string) (int, error) {\n\tl, err := net.Listen(\"tcp\", host+\":0\")\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"failed to assign a free port: %v\", err)\n\t}\n\tdefer l.Close()\n\tport := l.Addr().(*net.TCPAddr).Port\n\n\t// On Linux and some BSD variants, ephemeral ports are randomized, and may\n\t// consequently repeat within a short time frame after the listening end\n\t// has been closed. To avoid this, we make a connection to the port, then\n\t// close that connection from the server's side (this is very important),\n\t// which puts the connection in TIME_WAIT state for some time (by default,","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/temporalio/temporal/blob/bde624efd13fbd3843654058db6d9c716166318b/common/testing/freeport/freeport.go#L12-L48","documentation":"Error \"failed assigning ephemeral port: %w\" thrown in temporalio/temporal.","triggerScenarios":"Thrown at common/testing/freeport/freeport.go:30 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"bde624efd13fbd3843654058db6d9c716166318b","analyzedAt":"2026-09-01T07:18:39.080Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}