News for hackers. Stuff that matters.
Re:More powerful Go execution traces by the_gipsy on Friday March 15, 2024 @04:21AM (#39713285)
What about error stack traces? I find it crazy that you're supposed to grep error strings and pray they're all different.
Re:More powerful Go execution traces by pipe01 on Friday March 15, 2024 @05:07AM (#39713540)
You can do it yourself if you want using `runtime.Stack` and a custom error type
Re:More powerful Go execution traces by minzi on Friday March 15, 2024 @08:06AM (#39714618)
Embarrassingly, I’ve been writing Go for a while but never really thought about it. Now that it’s been mentioned I’m curious why this isn’t baked in by default for errors. Does anyone know?
Re:More powerful Go execution traces by sapiogram on Friday March 15, 2024 @09:24AM (#39715195)
> I’ve been writing Go for a while but never really thought about it.

Don't feel bad, I've tried to do this in some places, but I'm not sure it's worth it. It adds a ton of boilerplate to Go's already verbose error handling, since you need to wrap every error that gets returned from libraries.

Re:More powerful Go execution traces by icholy on Friday March 15, 2024 @10:23AM (#39716093)
Creating stack traces is expensive.
Re:More powerful Go execution traces by tbarbugli on Friday March 15, 2024 @01:28PM (#39718323)
you only do it when there is a not nil err + being able to have a stacktrace is worth more than whatever it costs is CPU
Re:More powerful Go execution traces by kbolino on Saturday March 16, 2024 @10:44AM (#39726389)
Good error-wrapping discipline works better than stack traces, because not only do you have a trace of the calling context, you also have the values that caused the problem. Granted, a stack trace "always works", but it will never have the values of important variables.
Re:More powerful Go execution traces by derekperkins on Saturday March 16, 2024 @04:52PM (#39729389)
We do both and it works great
Re:More powerful Go execution traces by Cthulhu_ on Monday March 18, 2024 @06:23AM (#39742148)
Now you mention it, a stack trace with function calls and all their arguments would be really powerful. But also expensive, ideally it would have zero overhead or only have the cost if you actually look at it / want to debug it.
Re:More powerful Go execution traces by kbolino on Monday March 18, 2024 @05:12PM (#39750087)
One of the major optimizations is to pass function arguments in registers instead of on the stack. These registers might preserve their original values by the time you unwind the stack, but in many cases, probably won't. Preserving those values would lead to fewer available registers and/or more frequent register saving, which would create at least some overhead, even if you don't ever inspect it.

There are probably lots of situations where it's worth it, though.

Re:More powerful Go execution traces by Cthulhu_ on Monday March 18, 2024 @06:22AM (#39742144)
That's still under the assumption that errors are exceptional, rare, and require a stacktrace to debug. But most errors are not exceptional and do not need debugging, like idk, a row not found error in a database.

A crash, sure, that might need a stacktrace to debug. But that's already in place.

Re:More powerful Go execution traces by closeparen on Friday March 15, 2024 @11:04AM (#39716564)
You’re supposed to prepend the context to an error before you return it, so the final error reads like a top-level stack trace: “handling request: fooing the bar: barring the baz: connecting to DB: timeout.”
Re:More powerful Go execution traces by kbolino on Saturday March 16, 2024 @10:30AM (#39726273)
Well, how would that work?

Errors are just values. They don't have any special meaning nor is there any special ceremony to create them. A panic must start from calling panic(); there's no function or special statement to create or return an error.

It might be possible to augment every `error`-typed return such that the concrete type is replaced by some synthetic type with a stack trace included. However, this would only work because `error` is an interface, so any function returning a concrete-typed error wouldn't be eligible for this; it would also add a lot of runtime overhead (including, at the very least, a check for nil on every return); and it would cause issues with code that still does == comparisons on errors.

On the whole, I think error-wrapping solves the problem of tracing an error well enough as the language exists today. If errors are going to start having some magic added to them, I think the entirety of error-handling deserves a rethink (which may be due, to be fair).

Re:More powerful Go execution traces by Xeoncross on Friday March 15, 2024 @09:51AM (#39715466)
Errors are different in every stdlib or 3rd party package I've ever used.

The ecosystem as a whole decided that errors should be useful, values, and properly bubbled up - and that your application should not be so hard to reason about that you need a stack trace to know where in the deep recesses of your code something went wrong.

I love stack traces in Java, but I don't miss them at all in Go.

Re:More powerful Go execution traces by the_gipsy on Friday March 15, 2024 @12:02PM (#39717267)
Sounds a bit apologetic. I don't think that the ecosystem decided. The language creators decided, and the ecosystem couldn't do much about it.
Re:More powerful Go execution traces by Xeoncross on Sunday March 17, 2024 @09:43AM (#39734437)
I say ecosystem to mean the majority of packages. That's the ecosystem.
Re:More powerful Go execution traces by Cthulhu_ on Monday March 18, 2024 @06:18AM (#39742127)
Go's development process is fairly open and democratic though; if enough people flag it up, and if a case can be made for stack traces, it can be added.

As it stands though, the consensus is that it doesn't add enough value, it's too expensive (cost/benefit), errors are normal program flow, and 3rd party libraries can add the functionality if one decides they need/want it in their application.

Re:More powerful Go execution traces by eweise on Friday March 15, 2024 @03:12PM (#39719584)
"your application should not be so hard to reason about that you need a stack trace to know where in the deep recesses of your code something went wrong."

Curious how Go is better than other languages in this regard.

Re:More powerful Go execution traces by Cthulhu_ on Monday March 18, 2024 @06:20AM (#39742132)
That quote isn't about Go specifically though, but about general application design. It seems to be a subtle snipe that languages with stacktraces like Java, C# are too complicated or have too many layers ("lasagna code"), making them more difficult to debug.
Re:More powerful Go execution traces by tbarbugli on Friday March 15, 2024 @01:31PM (#39718347)
we wrap all errors with a stack.Wrap function that adds stacktrace to the error. This allows us to add stacktraces to logs and err reporting tools like Sentry. Huge time saver