Permalink
Browse files

Make sure we kill the launched process after the request is done.

Seems like we re-invented CGI
  • Loading branch information...
1 parent 6840292 commit a5f9c9b499dc429f81af2f6f72260477c3348cb6 @tobi committed Dec 30, 2011
Showing with 6 additions and 13 deletions.
  1. +6 −13 main.go
View
19 main.go
@@ -1,7 +1,6 @@
package main
import (
- "io"
"os"
"fmt"
"bufio"
@@ -14,17 +13,9 @@ import (
"strconv"
)
-var port *int = flag.Int("port", 3333, "port")
-var executable *string = flag.String("executable", "server", "executable to launch. Must support -pXXXX for port assignment")
-var compiler *string = flag.String("compiler", "make", "compiler to execute. make by default.")
-
-func error(client io.WriteCloser, s string) {
- log.Printf("Error: %s", s)
- client.Write([]byte("HTTP/1.0 500 Internal Error\n"))
- client.Write([]byte("Content-Type: text/plain\n"))
- client.Write([]byte("\n\n" + s + "\n"))
- client.Close()
-}
+var port *int = flag.Int("p", 3333, "port")
+var executable *string = flag.String("e", "server", "executable to launch. Must support -pXXXX for port assignment")
+var compiler *string = flag.String("c", "gb", "compiler to execute. gb by default.")
func pipeRequestResponse(server, client net.Conn) os.Error {
@@ -72,7 +63,7 @@ func forward(client net.Conn) {
return
}
- client_port := *port+1024
+ client_port := *port + 1024
cmd = exec.Command(*executable, "-p", strconv.Itoa(client_port))
err = cmd.Start()
@@ -82,6 +73,8 @@ func forward(client net.Conn) {
return
}
+ defer cmd.Process.Kill()
+
// 20 retries, ~= 10 secs of attempts
for i := 0; i < 50; i++ {
server, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", client_port))

0 comments on commit a5f9c9b

Please sign in to comment.