惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V2EX - 技术
V2EX - 技术
P
Privacy International News Feed
Security Latest
Security Latest
H
Hacker News: Front Page
T
Tenable Blog
The Hacker News
The Hacker News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Security @ Cisco Blogs
Project Zero
Project Zero
O
OpenAI News
AI
AI
Spread Privacy
Spread Privacy
C
CERT Recently Published Vulnerability Notes
The Last Watchdog
The Last Watchdog
G
GRAHAM CLULEY
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Scott Helme
Scott Helme
Application and Cybersecurity Blog
Application and Cybersecurity Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
CXSECURITY Database RSS Feed - CXSecurity.com
NISL@THU
NISL@THU
A
Arctic Wolf
T
Threat Research - Cisco Blogs
PCI Perspectives
PCI Perspectives
N
News and Events Feed by Topic
C
Cyber Attacks, Cyber Crime and Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
Simon Willison's Weblog
Simon Willison's Weblog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
Google Online Security Blog
Google Online Security Blog
罗磊的独立博客
L
LINUX DO - 最新话题
U
Unit 42
S
Security Affairs
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
博客园 - 【当耐特】
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
月光博客
月光博客
Engineering at Meta
Engineering at Meta
腾讯CDC
F
Full Disclosure
Cyberwarzone
Cyberwarzone
S
SegmentFault 最新的问题
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 司徒正美
The Cloudflare Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
Goroutine Scheduling: GMP Model, Schedule Loop, Preemption & Stack Management
James Lee · 2026-05-19 · via DEV Community

The Go scheduler is one of the most sophisticated pieces of the runtime. It manages thousands of goroutines across a handful of OS threads, with minimal overhead and near-zero context-switch cost. In this article we'll trace the full lifecycle of a goroutine — from creation to scheduling loop to preemption to cleanup.


1. System Call vs Function Call: The Fundamental Difference

Before diving into the scheduler, it's worth understanding why goroutine scheduling requires assembly — and why it's fundamentally different from a regular function call.

Function Call System Call
Instruction CALL SYSCALL / INT 0x80
Address space User space only Crosses into kernel space
How to invoke By function name/address By integer syscall number
Registers saved PC + SP only All CPU registers (full context)
Virtual memory Not touched Not touched (same process)
Process switch No No (same process, intra-process switch)

On AMD64 Linux, the syscall convention is:

  • rax → syscall number
  • rdi, rsi, rdx, r10, r8, r9 → first 6 arguments

Why assembly for goroutine switching? Because switching execution flows requires precisely controlling PC (program counter) and SP (stack pointer) registers — something no high-level language can do. The scheduler's gogo, mcall, and goexit are all written in assembly.


2. The GMP Model

Go's scheduler is built on three core abstractions:

┌─────────────────────────────────────────────────────────┐
│                    GMP Model                            │
│                                                         │
│   G (Goroutine)   — the unit of work                    │
│   M (Machine)     — OS thread, the actual executor      │
│   P (Processor)   — scheduling context, owns LRQ        │
│                                                         │
│   P0          P1          P2                            │
│  [LRQ]       [LRQ]       [LRQ]    ← local run queues   │
│    │           │           │                            │
│    M0          M1          M2      ← OS threads         │
│    │           │           │                            │
│    G           G           G       ← running goroutines │
│                                                         │
│  [Global Run Queue]  ← shared, requires lock            │
│  [netpoller]         ← waiting on I/O                   │
└─────────────────────────────────────────────────────────┘

Enter fullscreen mode Exit fullscreen mode

Key design: mcache lives on P, not M. When M+G enter a blocking syscall and P detaches, the new M that picks up P inherits its mcache — ensuring lock-free allocation continues.


3. Initialization: From g0 to main

Before any user code runs, the runtime bootstraps itself:

Step 1: Initialize g0
─────────────────────────────────────────────
g0 is the "runtime goroutine" — provides a stack
for the runtime itself to execute on.
m0 ←→ g0 are bound together.
get_tls() on main thread → returns g0 → g0.m → m0

Step 2: Initialize P and allp
─────────────────────────────────────────────
schedinit() → mcommoninit() → procresize()
Creates GOMAXPROCS P structs, stored in allp[]

Step 3: Create main goroutine (newg)
─────────────────────────────────────────────
runtime.newproc() creates newg with 2KB stack
newg.stack.hi / newg.stack.lo mark stack bounds
Stack is primed: goexit's address pushed as fake return
newg.sched.pc = runtime.main
newg state → _Grunnable → placed in P's LRQ

Step 4: gogo — switch from g0 to newg
─────────────────────────────────────────────
Save g0's SP to g0.sched.sp
schedule() → finds newg
gogo() → switches stack from g0 to newg
         → JMP to newg.sched.pc (runtime.main)

Enter fullscreen mode Exit fullscreen mode

runtime.main startup sequence:

runtime.main()
    ├── start sysmon thread
    ├── init runtime packages
    ├── init all imported packages
    ├── call main.main()
    └── exit() syscall → process ends

Enter fullscreen mode Exit fullscreen mode

Note: Non-main goroutines return to goexit after finishing. The main goroutine terminates the entire process when it returns — this is the key difference.


4. The Scheduling Loop

Every M runs an infinite scheduling loop. In pseudocode:

// Each OS thread runs this loop
func scheduleLoop() {
    for {
        g := findRunnableGoroutine()  // find next G
        run(g)                         // execute G
        saveState(g)                   // save registers
    }
}

Enter fullscreen mode Exit fullscreen mode

The actual schedule() function searches for a runnable G in priority order:

func schedule() {
    // 1. Every 61 ticks: check global run queue (fairness guarantee)
    if schedtick % 61 == 0 && sched.runqsize > 0 {
        gp = globrunqget(p, 1)
    }
    // 2. Check P's local run queue (LRQ)
    if gp == nil {
        gp, inheritTime = runqget(p)
    }
    // 3. findrunnable(): steal from other Ps, check netpoller, block if nothing
    if gp == nil {
        gp, inheritTime = findrunnable()  // blocks until a G is found
    }
    // 4. Execute the G
    execute(gp, inheritTime)
}

Enter fullscreen mode Exit fullscreen mode

Fairness mechanism: Every 61 scheduling ticks, the scheduler checks the global queue first. Without this, goroutines in the global queue could starve if local queues are always full.

execute()gogo() → user goroutine

func execute(gp *g, inheritTime bool) {
    casgstatus(gp, _Grunnable, _Grunning)  // G state: Runnable → Running
    gp.preempt = false
    _g_.m.curg = gp   // M → G (forward link)
    gp.m = _g_.m      // G → M (back link)
    gogo(&gp.sched)   // assembly: switch stack + JMP to gp's PC
}

Enter fullscreen mode Exit fullscreen mode

The Full Scheduling Cycle

schedule()
    → execute()
        → gogo()          ← assembly: switch to G's stack
            → user func() ← G runs
            → goexit()    ← fake return (injected by newproc)
                → goexit1()
                    → mcall(goexit0)
                        → goexit0()
                            → casgstatus(_Grunning → _Gdead)
                            → dropg()        ← unbind M and G
                            → gfput(p, gp)   ← recycle G into gfree list
                            → schedule()     ← start next cycle  ♻️

Enter fullscreen mode Exit fullscreen mode

This is the complete loop: schedule → execute → gogo → user code → goexit → schedule.


5. Goroutine Preemption

Go uses a hybrid cooperative + preemptive scheduling model.

Cooperative Preemption (Go ≤ 1.13)

  • sysmon detects G running > 10ms → sets g.preempt = true
  • At every function call, the compiler inserts a stack-growth check (runtime.morestack_noctxt)
  • If preempt == true, G detaches from M and is pushed to the global queue

The loophole:

func main() {
    go fmt.Println("hi")
    for {}  // no function calls → preempt flag never checked → goroutine never yields
}

Enter fullscreen mode Exit fullscreen mode

Signal-Based Async Preemption (Go ≥ 1.14)

Go 1.14 introduced true async preemption using OS signals:

Step 1: M registers sighandler for SIGURG at startup

Step 2: sysmon detects G running > 10ms
        → calls preemptone()
        → sends SIGURG to the M running that G

Step 3: M's sighandler fires
        → receives sigPreempt
        → calls doSigPreempt()
        → injects a CALL to runtime.asyncPreempt
          into G's execution context (modifies PC)

Step 4: G executes asyncPreempt (without knowing it)
        → mcall → switches to g0 stack
        → gopreempt_m() → schedule()

Step 5: G is preempted and re-queued
        When rescheduled, G resumes from original PC  ✅

Enter fullscreen mode Exit fullscreen mode

Why SIGURG? It's rarely used by application code, making it a safe signal to hijack for the runtime.

This mechanism also benefits the GC: to stop the world, Go simply sends SIGURG to all running threads — no need for safe-point polling.


6. gopark: Voluntarily Suspending a Goroutine

When a goroutine needs to wait (channel, mutex, I/O), it calls gopark:

func gopark(unlockf func(*g, unsafe.Pointer) bool, ...) {
    mp := acquirem()
    gp := mp.curg
    mp.waitunlockf = unlockf
    gp.waitreason = reason
    releasem(mp)
    mcall(park_m)   // switch to g0, call park_m
}

func park_m(gp *g) {
    casgstatus(gp, _Grunning, _Gwaiting)  // G: Running → Waiting
    dropg()                                // unbind M and G
    if fn := _g_.m.waitunlockf; fn != nil {
        ok := fn(gp, _g_.m.waitlock)
        if !ok {
            // condition already met, re-run immediately
            casgstatus(gp, _Gwaiting, _Grunnable)
            execute(gp, true)
        }
    }
    schedule()  // find next G to run
}

Enter fullscreen mode Exit fullscreen mode

G state transitions:

_Grunnable ──execute()──► _Grunning
    ▲                          │
    │                     gopark() / syscall
    │                          ▼
goready()              _Gwaiting
    ▲                          │
    └──── event fires ─────────┘

Enter fullscreen mode Exit fullscreen mode


7. Goroutine Stacks

Unlike OS threads (which have fixed-size stacks, typically 1–8MB), goroutine stacks are dynamic and managed by the runtime.

type stack struct {
    lo uintptr   // stack bottom
    hi uintptr   // stack top
}

Enter fullscreen mode Exit fullscreen mode

  • Initial size: 2KB (allocated from stackpool or mcache)
  • Growth: When a function call would overflow the stack, the compiler-inserted morestack check triggers a stack copy to a larger allocation
  • Small stacks (< threshold): Allocated from stackpool / mcache.stackcache
  • Large stacks: Allocated from stackLarge or directly from mheap

Goroutine stacks vs OS thread stacks: OS thread stacks are part of the process memory model (task_struct). Goroutine stacks are runtime-allocated heap memory, referenced by the g.stack field.


8. Goroutine Leaks

A goroutine leak occurs when a goroutine starts but never exits — typically because it's blocked forever on a channel, mutex, or I/O that never resolves.

Why leaks are serious:

  • Each goroutine holds at least 2KB of stack memory
  • Heap objects referenced by the goroutine cannot be GC'd
  • Memory usage grows continuously → eventual OOM crash

Common leak patterns:

// Leak: channel send with no receiver
func leak() {
    ch := make(chan int)
    go func() {
        ch <- 1  // blocks forever if no one reads
    }()
    // ch is never read
}

// Leak: infinite loop with no exit condition
func leak2(done <-chan struct{}) {
    go func() {
        for {
            // forgot to check <-done
        }
    }()
}

Enter fullscreen mode Exit fullscreen mode

Detection: Use go tool pprof → goroutine profile to see all live goroutines and their creation stacks.


9. Summary: The Complete Scheduling Picture

go func() { ... }
      ↓
runtime.newproc() → allocate G, push to P's LRQ
      ↓
M's schedule loop picks up G
      ↓
execute() → gogo() → G runs on M
      ↓
Preemption? ──► SIGURG → asyncPreempt → schedule()
Blocking?   ──► gopark() → _Gwaiting → schedule()
Syscall?    ──► entersyscall → P detaches → exitsyscall
Done?       ──► goexit() → goexit0() → gfput() → schedule()

Enter fullscreen mode Exit fullscreen mode

Concept Role
schedule() Find next runnable G (LRQ → global queue → steal → netpoller)
execute() Set G state to Running, bind M↔G, call gogo
gogo() Assembly: switch stack + JMP to G's PC
goexit() Cleanup after G finishes: reset, recycle, re-schedule
gopark() Voluntarily suspend G: Running → Waiting → schedule()
sysmon Background monitor: preempt long Gs, reclaim stuck Ps
Signal preemption SIGURG → inject asyncPreempt → force schedule()

Next in this series: Go Compiler & defer: Bootstrap Compiler, pprof+trace Toolchain & defer Internals (Part 6)


Follow the series for more deep dives into Go's runtime internals.