Skip to content

Server panicked when asking serve again #1472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
moaz-mokhtar opened this issue Feb 22, 2021 · 3 comments
Open

Server panicked when asking serve again #1472

moaz-mokhtar opened this issue Feb 22, 2021 · 3 comments
Labels
A-Errors Area: Errors and warnings

Comments

@moaz-mokhtar
Copy link

Hello all,

Very thankful for this great tool.

While I'm testing mdbook for the rust-clippy, I found issues as below steps:

  • I requested a command serve to open the book in the localhost.
    image

  • I opened http://127.0.0.1:3000, after that I did press Ctrl+z to stop the command.
    image

  • When I did serve command again, it is panicked which seems server haven't terminated.
    image

  • Both servers are not opening the book.
    image

@moaz-mokhtar
Copy link
Author

For your info I did check the ports by bash command sudo netstat -tulpn | grep LISTEN as per link, for localhost and found that it was still opened as below:
image

I did below command fuser -n tcp -k 3000 to close the port, as per link.
image

@ehuss
Copy link
Contributor

ehuss commented Jul 27, 2021

There is not much mdbook can do in this case, since you already had another server already running. Operating systems will not allow multiple processes to bind to the same address. Perhaps the error could give a suggestion to search for a running process or otherwise explain the situation better.

@ehuss ehuss added the A-Errors Area: Errors and warnings label Jul 27, 2021
@kg4zow
Copy link
Contributor

kg4zow commented May 31, 2022

@moaz-mokhtar Your first screenshot shows that you hit ^Z, and it said that the process is "Stopped".

In this context, the term "Stopped" doesn't mean what most people think it means. The process still exists, and the resources it's using (RAM, file handles, sockets, signal handlers, etc.) are still allocated to the process. The kernel isn't giving the process any CPU cycles so it isn't actively "running", but it does still exist. (I've always thought they should have called this state "suspended".)

When a process is in this state, you can "start" (or "resume") it again, and it will pick right back up where it left off. Any outstanding signals will be delivered to the process, and it will "catch up" on whatever it missed while it was suspended.

For example, I'm going to start a process, and then use ^Z to stop (or suspend) it.

$ mdbook serve --open --hostname 127.0.0.1
2022-05-31 00:48:46 [INFO] (mdbook::book): Book building has started
2022-05-31 00:48:46 [INFO] (mdbook::book): Running the html backend
2022-05-31 00:48:46 [INFO] (mdbook::cmd::serve): Serving on: http://127.0.0.1:3000
2022-05-31 00:48:46 [INFO] (mdbook): Opening web browser
2022-05-31 00:48:46 [INFO] (warp::server): Server::run; addr=127.0.0.1:3000
2022-05-31 00:48:46 [INFO] (warp::server): listening on http://127.0.0.1:3000
2022-05-31 00:48:47 [INFO] (mdbook::cmd::watch): Listening for changes...
^Z
[1]+  Stopped                 mdbook serve --open --hostname 127.0.0.1

The jobs command will show you all child processes under the current shell. As you can see, the process still exists, even if it isn't running (using CPU cycles, watching files, handling network traffic to/from your browser, etc.)

$ jobs
[1]+  Stopped                 mdbook serve --open --hostname 127.0.0.1

The fg (foreground) command will "un-stop" (or resume) a process. It uses the "job number", which is in square brackets at the beginning of each line. (In your case above it was 2, but in this example it's 1).

$ fg %1
mdbook serve --open --hostname 127.0.0.1

At this point the process is running again. I'm going to flip over to my text editor and make a change to a file, and when I hit SAVE ...

2022-05-31 00:49:00 [INFO] (mdbook::cmd::serve): Files changed: ["xxx/src/introduction.md"]
2022-05-31 00:49:00 [INFO] (mdbook::cmd::serve): Building book...
2022-05-31 00:49:00 [INFO] (mdbook::book): Book building has started
2022-05-31 00:49:00 [INFO] (mdbook::book): Running the html backend

... it sees the change and rebuilds the HTML, as expected.

Again, the term "stop" is confusing here. I suspect that what you originally wanted to do was terminate the process, so it no longer exists, and the resources it was using (including the socket that was listening on port 3000) are released.

⚠️ Use ^C to terminate a process.

...
2022-05-31 00:54:17 [INFO] (mdbook::book): Book building has started
2022-05-31 00:54:17 [INFO] (mdbook::book): Running the html backend
^C
$ jobs
$ 

Also ... if you go back and read the error message in your screenshot, you'll see that it contains error binding to 127.0.0.1:3000: error creating server listener: Address already in use. Your "stopped" process still had an open socket listening on that IP:PORT, so the new process wasn't able to listen to it. You would get the same message if you run mdbook serve in two different windows at the same time.

Hopefully this clears things up a bit for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Errors Area: Errors and warnings
Projects
None yet
Development

No branches or pull requests

3 participants