Failed to initialize replica

yage@ligexi:~$ dfx start -v
Running dfx start for version 0.24.2
There is no project-specific network ‘local’ because there is no project (no dfx.json).
Using shared network ‘local’ defined in /home/yage/.config/dfx/networks.json
Local server configuration:
bind address: 127.0.0.1:4943
bitcoin: disabled
canister http: enabled
log level: Error
replica:
subnet type: Application
log level: Error
data directory: /home/yage/.local/share/dfx/network/local
scope: shared

Starting canister http adapter…
canister http adapter ready
Starting replica…
Waiting for replica to report healthy status
ERROR: Failed to initialize replica: The replica returned an HTTP Error: Http Error: status 500 Internal Server Error, content type “text/plain; charset=utf-8”, content: connection rejected: dial tcp 127.0.0.1:41283: connect: connection refused
Does anyone know how to solve this?

Have you tried running dfx start with --clean?

yage@ligexi:~$ dfx start --clean --host 0.0.0.0:8000 -vv
Trace mode enabled. Lots of logs coming up.
Running dfx start for version 0.24.2
There is no project-specific network ‘local’ because there is no project (no dfx.json).
Using shared network ‘local’ defined in /home/yage/.config/dfx/networks.json
Local server configuration:
bind address: 0.0.0.0:8000 (default: 127.0.0.1:4943)
bitcoin: disabled
canister http: enabled
log level: Error
replica:
port: 8000
subnet type: System (default: Application)
log level: Error
data directory: /home/yage/.local/share/dfx/network/local
scope: shared

Starting canister http adapter…
canister http adapter ready
Starting replica…
Waiting for replica to report healthy status
ERROR: Failed to initialize replica: An error happened during communication with the replica: error sending request for url (localhost:8000/api/v2/status): error sending request for url (localhost:8000/api/v2/status): client error (Connect): tcp connect error: Connection refused (os error 111): Connection refused (os error 111)
Starting replica…
Waiting for replica to report healthy status

What’s in /home/yage/.config/dfx/networks.json for the local network?

If the local network definition in networks.json specifies port 8000 in the replica section, I suggest to remove that.

yage@ligexi:~/.config/dfx$ cat networks.json
{
“local”: {
“bind”: “127.0.0.1:4943”
}
}

The cause, somehow, is most likely that the replica port is set to 8000. It can’t be the same as the host port.

Given your ~/.config/dfx/networks.json, and the fact that dfx start reports that there is no dfx.jon, I don’t have an explanation for this in the output that you’re seeing:

replica:
  port: 8000
  subnet type: System (default: Application)

Here is what I get. Notice under “Local server configuration” section there are no overrides for the port or subnet type.

% head -n 4 ~/.config/dfx/networks.json ; dfx +0.24.2 start --clean --host 0.0.0.0:8000 -vv
{
  "local": {
    "bind": "127.0.0.1:4943"
  },
Trace mode enabled. Lots of logs coming up.
Running dfx start for version 0.24.2
There is no project-specific network 'local' because there is no project (no dfx.json).
Using shared network 'local' defined in /Users/ericswanson/.config/dfx/networks.json
Local server configuration:
  bind address: 0.0.0.0:8000 (default: 127.0.0.1:4943)
  bitcoin: disabled
  canister http: enabled
    log level: Error
  replica:
    subnet type: Application
    log level: Error
  data directory: /Users/ericswanson/Library/Application Support/org.dfinity.dfx/network/local
  scope: shared

Starting canister http adapter...
canister http adapter ready
Starting replica...
Waiting for replica to report healthy status
Initialized replica.
Dashboard: http://localhost:50758/_/dashboard
replica ready on http://localhost:50758, so re/starting HTTP gateway
Starting pocket-ic gateway...
2024-11-26T12:14:52.682726Z  INFO pocket_ic_server: The PocketIC server is listening on port 50761
Configuring PocketIC gateway
Initialized HTTP gateway.
Replica API running on 0.0.0.0:8000

The “turn it off and turn it on again” approach would be dfx stop, then delete /home/yage/.local/share/dfx/network/local , then try the original dfx start -v that you were attempting.

yage@ligexi:~/.local/share/dfx/network$ rm -rf local/
yage@ligexi:~/.local/share/dfx/network$ dfx start -v
Running dfx start for version 0.24.2
There is no project-specific network ‘local’ because there is no project (no dfx.json).
Using shared network ‘local’ defined in /home/yage/.config/dfx/networks.json
Local server configuration:
bind address: 127.0.0.1:4943
bitcoin: disabled
canister http: enabled
log level: Error
replica:
subnet type: Application
log level: Error
data directory: /home/yage/.local/share/dfx/network/local
scope: shared

Starting canister http adapter…
canister http adapter ready
Starting replica…
Waiting for replica to report healthy status
ERROR: Failed to initialize replica: An error happened during communication with the replica: error sending request for url (localhost:36799/api/v2/status): error sending request for url (localhost:36799/api/v2/status): client error (Connect): tcp connect error: Connection refused (os error 111): Connection refused (os error 111)
Starting replica…
Waiting for replica to report healthy status

My machine environment: Win11 + WSL + Ubuntu

yage@ligexi:~$ ps -ef | grep replica
yage 4451 3930 10 21:07 pts/2 00:00:05 /home/yage/.cache/dfinity/versions/0.24.2/replica --replica-version 0.9.0 --config-file /home/yage/.local/share/dfx/network/local/2282cb9cc08e17798da9997dc3cc74c53ef04d5cf51307505bad84a126c54f61/state/replicated_state/ic.json5