Skip to content

RunXrayFromJSON starts Xray core twice leading to "address already in use" error #109

@kibsoft

Description

@kibsoft

When using RunXrayFromJSON, the Xray core appears to be started twice, which results in a TCP port binding error.
Despite this error, Xray continues to work normally and accepts connections, but the function returns an error as if Xray failed to start. This makes it impossible for the caller to reliably determine whether Xray is actually running.

{
  "success": false,
  "error": "app/proxyman/inbound: failed to listen TCP on 14555 > transport/internet: failed to listen on address: 127.0.0.1:14555 > transport/internet/tcp: failed to listen TCP on 127.0.0.1:14555 > listen tcp 127.0.0.1:14555 : bind: Only one usage of each socket address (protocol/network address/port) is normally permitted."
}

If coreServer.Start() is commented out, Xray starts and works correctly.
This suggests that StartXrayFromJSON(configJSON) already starts the core internally, and calling coreServer.Start() again causes a second attempt to bind to the same port.

func RunXrayFromJSON(datDir string, configJSON string) (err error) {

func RunXrayFromJSON(datDir string, configJSON string) (err error) {
	InitEnv(datDir)
	memory.InitForceFree()

	coreServer, err = StartXrayFromJSON(configJSON)
	if err != nil {
		return
	}

	// This call causes a second start
	if err = coreServer.Start(); err != nil {
		return
	}

	debug.FreeOSMemory()
	return nil
}

Environment

OS: Windows 11
Xray version: 25.12.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions