Explain netpoll on Linux, BSD/macOS and Windows
Explain netpoll on Linux, BSD/macOS and Windows
The network poller connects waiting goroutines to OS I/O notifications. In Go 1.23 on Linux, epoll reports descriptor readiness; on BSD and macOS, kqueue provides event notifications. Windows uses an I/O completion port (IOCP) to receive completion notifications for overlapped I/O, rather than the same readiness model. The runtime and internal/poll coordinate waits and deadlines so many network waits need not each occupy an OS thread. Not every file descriptor or blocking system call is handled this way. These are implementation details; applications should use net.Conn and context-aware APIs.
package main
import ( "fmt" "io" "net" "time")
func main() { ln, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { panic(err) } defer ln.Close() done := make(chan error, 1) go func() { c, e := ln.Accept() if e != nil { done <- e return } defer c.Close() if e = c.SetDeadline(time.Now().Add(3 * time.Second)); e != nil { done <- e return } _, e = c.Write([]byte("ok")) done <- e }() c, e := net.DialTimeout("tcp", ln.Addr().String(), 3*time.Second) if e != nil { panic(e) } defer c.Close() if e = c.SetDeadline(time.Now().Add(3 * time.Second)); e != nil { panic(e) } b := make([]byte, 2) if _, e = io.ReadFull(c, b); e != nil { panic(e) } if e = <-done; e != nil { panic(e) } fmt.Println(string(b))}The listener chooses an unused local port; the client reads exactly two bytes. A TCP stream does not preserve write boundaries, so ReadFull is appropriate. Deadlines bound the socket operations in this isolated lab. This exercises the local TCP stack, not a direct observation of epoll events. Actual readiness does not guarantee that an entire application message is available.
Expected output:
okInspect timeout through the public contract
package main
import ( "errors" "fmt" "net" "os" "time")
func main() { a, b := net.Pipe() defer a.Close() defer b.Close() if e := a.SetReadDeadline(time.Now().Add(-time.Second)); e != nil { panic(e) } _, e := a.Read(make([]byte, 1)) var ne net.Error fmt.Println(errors.As(e, &ne) && ne.Timeout(), errors.Is(e, os.ErrDeadlineExceeded))}An already-expired deadline produces a timeout without depending on sleep durations. net.Pipe is an in-memory connection and does not exercise OS polling. A connection deadline affects pending and future operations until reset. Clear a deadline with the zero time when reuse is intentional; a context cancellation does not automatically interrupt arbitrary net.Conn reads. Use deadlines or close as appropriate.
Expected output:
true trueTry it locally
Create an in-memory pipe, close one endpoint, and show that reading the other returns EOF. Set a deadline first as a watchdog.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Runtime representations 05: Explain netpoll on Linux, BSD/macOS and Windows
Check the lesson objective before moving on.
Which mechanism is used by Go 1.23 on Windows for network completion notifications?
Why is net.Pipe not a test of epoll or kqueue?