5

As i learned from golang docs, if i set runtime.GOMAXPROCS(8) with a cpu of 8 cores (intel i7), then start a goroutine of infinite-loop, other gorutines should not be blocked because there are engough threads and goprocs. But this is not true when using net/http package, an infinite-loop goroutine will block http server after a few invocations. Can anyone help to explain why ?

  1. If i comment the line of "go infinite loop", start client after server, client will output 1000 asterisks; but if i enable the goroutine, client will block after print a few asterisks
  2. I have tried add runtime.LockOSThread() in the goroutine, it seems that doesn't work
  3. My Environment: osx 10.10, go version go1.3.1 darwin/amd64

Server code:

package main

import (
    "fmt"
    "log"
    "net/http"
    "runtime"
)

func myHandler(w http.ResponseWriter, req *http.Request) {
    w.Write([]byte("hello"))
}

func infiniteloop() {
    for {

    }
}

func main() {
    // set max procs for multi-thread executing
    runtime.GOMAXPROCS(runtime.NumCPU())

    // print GOMAXPROCS=8 on my computer
    fmt.Println("GOMAXPROCS=", runtime.GOMAXPROCS(-1))
    http.Handle("/", http.HandlerFunc(myHandler))

    // uncomment below line cause server block after some requests 
    // go infiniteloop()
    if err := http.ListenAndServe(":8280", nil); err != nil {
        log.Fatal(err)
    }
}

Client code:

package main
import (
    "fmt"
    "net/http"
)

func getOnce() {
    if resp, err := http.Get("http://localhost:8280"); err != nil {
        fmt.Println(err)
        return
    } else {
        defer func() {
            if err := resp.Body.Close(); err != nil {
                fmt.Println(err)
            }
        }()
        if resp.StatusCode != 200 {
            fmt.Println("error codde:", resp.StatusCode)
            return
        } else {
            fmt.Print("*")

        }
    }
}

func main() {
    for i := 1; i < 1000; i++ {
        getOnce()
        if i%50 == 0 {
            fmt.Println()
        }
    }

}

Now i know why such emtpy loop block other goroutines, but why runtime.LockOSThread() doesn't help either?

func infiniteloop() {
    // add LockOSThread will not help
    runtime.LockOSThread()
    for {
    }
}

As http://golang.org/pkg/runtime/#LockOSThread mentioned, the empty loop should be executed in an standalone thread, and other goroutines should not be impacted by the busy loop. What's wrong in my understanding?

5
  • An "infinite" loop isn't a problem. It's a busy loop that does nothing but burn CPU that's a problem.
    – JimB
    Oct 29, 2014 at 12:14
  • @Grzegorz Żur Are you sure the client can print all 1000 asterisks? I have cross-compiled the source files and run them on an linux/amd64 platform. The client printed about 150 asterisks before halted
    – user922965
    Oct 29, 2014 at 14:01
  • @user922965 My mistake, I run it with critical line commented out. I redone the test and it blocks. Oct 29, 2014 at 14:29
  • I get the same halts, which is unexpected, must be a limitation of the scheduler somehow? Maybe file a bug report :)
    – rogerdpack
    Oct 29, 2014 at 16:11
  • I run the code with version go1.4beta1 linux/amd64 and it have not printed a single character. :-( I think it is a good question for golang-nuts. Oct 31, 2014 at 6:39

2 Answers 2

5

The Go runtime's scheduler is not fully pre-emptive at this time. Go 1.2 improved matters by occasionally calling into the scheduler on function calls, but the infinite loops in your example have no function calls so this doesn't help.

With an actual body to your infinite loop handlers, you may see better behaviour. Alternatively, a manual call to runtime.Gosched may help in cases like this.

4
  • I add one more question of LockOfThread, can you give more advices?
    – user922965
    Oct 29, 2014 at 14:38
  • Perhaps it would help to describe what you're trying to do exactly. Surely you aren't using empty infinite loops in your real program, so what exactly is it you want to know? Oct 30, 2014 at 1:36
  • I am trying to compare http performance between golang and java nio, this is an edge case that some cores are exhausted by other applications and can not be used by http server. Your links have resolved my question actually, i just wonder why "LockOSThread" doesn't work, and try to get more knowledge of golang scheduler.
    – user922965
    Oct 30, 2014 at 2:04
  • I'm not sure if LockOSThread actually allocates another thread for running Go code on or not. If it doesn't, then it wouldn't help here. If you want to experiment with reducing the number of cores your app runs on for testing, perhaps look at your OS's CPU affinity features. On Linux, the taskset utility might do the trick. Oct 30, 2014 at 2:14
-1

The scheduler might not be able to preempt such an empty "infinite" loop. The scheduler got better and better during the last release, maybe he should be good enough for such code; he definitely is good enough for real code. Just don't do such nonsense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.