[go] runtime: include throw and fatal message in SetCrashOutput

0 views
Skip to first unread message

Joseph Tsai (Gerrit)

unread,
Dec 16, 2025, 5:36:28 PM (9 hours ago) Dec 16
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Joseph Tsai has uploaded the change for review

Commit message

runtime: include throw and fatal message in SetCrashOutput

The throw and fatal message were unfortunately excluded
from the SetCrashOutput because the message was printed
before setting the releveant state needed to indicate
that the system is crashing.

Fix this by printing the message after setting m.throwing
and by having writeErrData emit the message
if the goroutine is in the process of throwing.

Fixes #76859
Change-Id: I36c1b3805cd0a8d0a7561cc655d801eea81a4c80

Change diff

diff --git a/src/runtime/panic.go b/src/runtime/panic.go
index d467e93..22b4291 100644
--- a/src/runtime/panic.go
+++ b/src/runtime/panic.go
@@ -1220,13 +1220,11 @@
func throw(s string) {
// Everything throw does should be recursively nosplit so it
// can be called even when it's unsafe to grow the stack.
- systemstack(func() {
+ fatalthrow(throwTypeRuntime, func() {
print("fatal error: ")
printindented(s) // logically printpanicval(s), but avoids convTstring write barrier
print("\n")
})
-
- fatalthrow(throwTypeRuntime)
}

// fatal triggers a fatal error that dumps a stack trace and exits.
@@ -1243,14 +1241,12 @@
// Everything fatal does should be recursively nosplit so it
// can be called even when it's unsafe to grow the stack.
printlock() // Prevent multiple interleaved fatal reports. See issue 69447.
- systemstack(func() {
+ fatalthrow(throwTypeUser, func() {
printPreFatalDeferPanic(p)
print("fatal error: ")
printindented(s) // logically printpanicval(s), but avoids convTstring write barrier
print("\n")
})
-
- fatalthrow(throwTypeUser)
printunlock()
}

@@ -1413,11 +1409,11 @@
}

// fatalthrow implements an unrecoverable runtime throw. It freezes the
-// system, prints stack traces starting from its caller, and terminates the
-// process.
+// system, calls preStackPrint, prints stack traces starting from its caller,
+// and terminates the process.
//
//go:nosplit
-func fatalthrow(t throwType) {
+func fatalthrow(t throwType, preStackPrint func()) {
pc := sys.GetCallerPC()
sp := sys.GetCallerSP()
gp := getg()
@@ -1429,6 +1425,12 @@
// Switch to the system stack to avoid any stack growth, which may make
// things worse if the runtime is in a bad state.
systemstack(func() {
+ // Print extra information after setting m.throwing so that
+ //it is also captured in the debug.SetCrashOutput file.
+ if preStackPrint != nil {
+ preStackPrint()
+ }
+
if isSecureMode() {
exit(2)
}
diff --git a/src/runtime/runtime.go b/src/runtime/runtime.go
index 016cbda..8aa9740 100644
--- a/src/runtime/runtime.go
+++ b/src/runtime/runtime.go
@@ -235,6 +235,7 @@
// If crashing, print a copy to the SetCrashOutput fd.
gp := getg()
if gp != nil && gp.m.dying > 0 ||
+ gp != nil && gp.m.throwing > 0 ||
gp == nil && panicking.Load() > 0 {
if fd := crashFD.Load(); fd != ^uintptr(0) {
write(fd, unsafe.Pointer(data), n)

Change information

Files:
  • M src/runtime/panic.go
  • M src/runtime/runtime.go
Change size: S
Delta: 2 files changed, 12 insertions(+), 9 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I36c1b3805cd0a8d0a7561cc655d801eea81a4c80
Gerrit-Change-Number: 730580
Gerrit-PatchSet: 1
Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
unsatisfied_requirement
satisfied_requirement
open
diffy

Joseph Tsai (Gerrit)

unread,
Dec 16, 2025, 5:38:55 PM (9 hours ago) Dec 16
to goph...@pubsubhelper.golang.org, Michael Pratt, Michael Knyszek, golang-co...@googlegroups.com
Attention needed from Michael Knyszek and Michael Pratt

Joseph Tsai added 1 comment

Patchset-level comments
File-level comment, Patchset 1 (Latest):
Joseph Tsai . resolved

I'm not sure what's the best way to test this, but happy to hear suggestions. I did test it locally with the example linked in the issue.

Open in Gerrit

Related details

Attention is currently required from:
  • Michael Knyszek
  • Michael Pratt
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I36c1b3805cd0a8d0a7561cc655d801eea81a4c80
Gerrit-Change-Number: 730580
Gerrit-PatchSet: 1
Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
Gerrit-Reviewer: Michael Knyszek <mkny...@google.com>
Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
Gerrit-Attention: Michael Pratt <mpr...@google.com>
Gerrit-Attention: Michael Knyszek <mkny...@google.com>
Gerrit-Comment-Date: Tue, 16 Dec 2025 22:38:50 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
unsatisfied_requirement
satisfied_requirement
open
diffy

Michael Knyszek (Gerrit)

unread,
Dec 16, 2025, 6:12:56 PM (8 hours ago) Dec 16
to Joseph Tsai, goph...@pubsubhelper.golang.org, Michael Pratt, golang-co...@googlegroups.com
Attention needed from Joseph Tsai and Michael Pratt

Michael Knyszek added 4 comments

Patchset-level comments
Joseph Tsai . resolved

I'm not sure what's the best way to test this, but happy to hear suggestions. I did test it locally with the example linked in the issue.

Michael Knyszek

the best thing would probably be to generalize TestSetCrashOutput https://cs.opensource.google/go/go/+/master:src/runtime/debug/stack_test.go;l=134;drc=dceee2e983f5dab65c3905ecf40e70e15cf41b7d, and crash for a different reason (like concurrent map writes), if you're up for it! otherwise just copying it is probably fine for now, we can clean it up later.

Michael Knyszek . resolved

thanks!

File src/runtime/panic.go
Line 1412, Patchset 1 (Latest):// system, calls preStackPrint, prints stack traces starting from its caller,
Michael Knyszek . unresolved

I think it would be helpful to explain why preStackPrint exists at all, since it's a bit odd. it's also worth documenting that the function will run on the system stack. (if it didn't, the preStackPrint should possibly be limited to nosplit functions. it's better if the caller doesn't have to wonder.)

I can't think of a nicer way to do this, and it does have upsides. we could set `gp.m.throwing` earlier via some other function, but that doesn't seem great either.

Line 1429, Patchset 1 (Latest): //it is also captured in the debug.SetCrashOutput file.
Michael Knyszek . unresolved

nit: need extra space.

Open in Gerrit

Related details

Attention is currently required from:
  • Joseph Tsai
  • Michael Pratt
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I36c1b3805cd0a8d0a7561cc655d801eea81a4c80
    Gerrit-Change-Number: 730580
    Gerrit-PatchSet: 1
    Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
    Gerrit-Reviewer: Michael Knyszek <mkny...@google.com>
    Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
    Gerrit-Attention: Michael Pratt <mpr...@google.com>
    Gerrit-Attention: Joseph Tsai <joe...@digital-static.net>
    Gerrit-Comment-Date: Tue, 16 Dec 2025 23:12:51 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Comment-In-Reply-To: Joseph Tsai <joe...@digital-static.net>
    unsatisfied_requirement
    open
    diffy

    Joseph Tsai (Gerrit)

    unread,
    Dec 16, 2025, 8:41:08 PM (6 hours ago) Dec 16
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
    Attention needed from Joseph Tsai and Michael Pratt

    Joseph Tsai uploaded new patchset

    Joseph Tsai uploaded patch set #2 to this change.
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Joseph Tsai
    • Michael Pratt
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: newpatchset
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I36c1b3805cd0a8d0a7561cc655d801eea81a4c80
    Gerrit-Change-Number: 730580
    Gerrit-PatchSet: 2
    unsatisfied_requirement
    open
    diffy

    Joseph Tsai (Gerrit)

    unread,
    Dec 16, 2025, 8:41:22 PM (6 hours ago) Dec 16
    to goph...@pubsubhelper.golang.org, Michael Pratt, Michael Knyszek, golang-co...@googlegroups.com
    Attention needed from Michael Knyszek and Michael Pratt

    Joseph Tsai voted and added 2 comments

    Votes added by Joseph Tsai

    Commit-Queue+1

    2 comments

    File src/runtime/panic.go
    Line 1412, Patchset 1:// system, calls preStackPrint, prints stack traces starting from its caller,
    Michael Knyszek . resolved

    I think it would be helpful to explain why preStackPrint exists at all, since it's a bit odd. it's also worth documenting that the function will run on the system stack. (if it didn't, the preStackPrint should possibly be limited to nosplit functions. it's better if the caller doesn't have to wonder.)

    I can't think of a nicer way to do this, and it does have upsides. we could set `gp.m.throwing` earlier via some other function, but that doesn't seem great either.

    Joseph Tsai

    Done

    Line 1429, Patchset 1: //it is also captured in the debug.SetCrashOutput file.
    Michael Knyszek . resolved

    nit: need extra space.

    Joseph Tsai

    Done

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Michael Knyszek
    • Michael Pratt
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I36c1b3805cd0a8d0a7561cc655d801eea81a4c80
      Gerrit-Change-Number: 730580
      Gerrit-PatchSet: 1
      Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
      Gerrit-Reviewer: Joseph Tsai <joe...@digital-static.net>
      Gerrit-Reviewer: Michael Knyszek <mkny...@google.com>
      Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
      Gerrit-Attention: Michael Pratt <mpr...@google.com>
      Gerrit-Attention: Michael Knyszek <mkny...@google.com>
      Gerrit-Comment-Date: Wed, 17 Dec 2025 01:41:18 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: Yes
      Comment-In-Reply-To: Michael Knyszek <mkny...@google.com>
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Joseph Tsai (Gerrit)

      unread,
      Dec 16, 2025, 8:42:35 PM (6 hours ago) Dec 16
      to goph...@pubsubhelper.golang.org, Michael Pratt, Michael Knyszek, golang-co...@googlegroups.com
      Attention needed from Michael Knyszek and Michael Pratt

      Joseph Tsai added 1 comment

      Patchset-level comments
      Joseph Tsai . resolved

      I'm not sure what's the best way to test this, but happy to hear suggestions. I did test it locally with the example linked in the issue.

      Michael Knyszek

      the best thing would probably be to generalize TestSetCrashOutput https://cs.opensource.google/go/go/+/master:src/runtime/debug/stack_test.go;l=134;drc=dceee2e983f5dab65c3905ecf40e70e15cf41b7d, and crash for a different reason (like concurrent map writes), if you're up for it! otherwise just copying it is probably fine for now, we can clean it up later.

      Joseph Tsai

      Thanks for the tip. I was able to adjust the test, but I needed to use linkname to access `fatalthrow`. Trying to trigger one felt a bit flaky.

      Open in Gerrit

      Related details

      Attention is currently required from:
      • Michael Knyszek
      • Michael Pratt
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I36c1b3805cd0a8d0a7561cc655d801eea81a4c80
      Gerrit-Change-Number: 730580
      Gerrit-PatchSet: 2
      Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
      Gerrit-Reviewer: Joseph Tsai <joe...@digital-static.net>
      Gerrit-Reviewer: Michael Knyszek <mkny...@google.com>
      Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
      Gerrit-Attention: Michael Pratt <mpr...@google.com>
      Gerrit-Attention: Michael Knyszek <mkny...@google.com>
      Gerrit-Comment-Date: Wed, 17 Dec 2025 01:42:31 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      Comment-In-Reply-To: Michael Knyszek <mkny...@google.com>
      Comment-In-Reply-To: Joseph Tsai <joe...@digital-static.net>
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy
      Reply all
      Reply to author
      Forward
      0 new messages