Commit graph

15 commits

Author SHA1 Message Date
CJ Horton
6e1e00ae3f standardize panic output
Programs that monitor Terraform's output to report panics might
make the reasonable assumption that the string "panic:" is always
included and is therefore safe to monitor for. Our custom panic
output unfortunately breaks these assumptions at the moment.

Instead of asking consumers to add their own handling to deal with
this problem, let's add that greppable string to our custom panic
output.
2024-02-14 12:46:20 -08:00
Martin Atkins
e6ce1edf22 logging: Send captured panics to real stderr
Our goal with this panic-interception was to largely mimic how the Go
runtime would normally report panics except for two intentional
exceptions: an extra prompt explaining to the user that Terraform crashed,
and exiting with status code 11 instead of 2.

Unfortunately we accidentally deviated in a different way: we're reporting
to whatever os.Stderr happens to refer to, instead of to the real process
stderr. It seems like that shouldn't really matter, but unfortunately
go-plugin intentionally changes os.Stderr to refer to a totally separate
stream that it manages, causing the captured panic messages to be routed
over a grpc-based channel to the plugin client.

This deviation makes the panic messages not visible to usual strategies
for trying to heuristically detect that a Go program has panicked. Without
a special interception like Terraform is doing here, the Go runtime
writes directly to the stderr file descriptor without going through the
os.Stderr abstraction, and so to achieve consistent behavior we need to
do a little hoop-jumping to approximate that result.

In particular, this makes the behavior now consistent with what happens
when a provider plugin running as a child of Terraform Core panics, and
so a system which tries to sniff stderr for content that seems like a
panic message will be able to handle both situations equally and avoid
making a special case for Terraform Core/CLI's own panics.
2024-02-12 12:11:20 -08:00
hashicorp-copywrite[bot]
53c34ff49c
Update copyright file headers to BUSL-1.1 2023-08-10 23:43:27 +01:00
hashicorp-copywrite[bot]
325d18262e [COMPLIANCE] Add Copyright and License Headers 2023-05-02 15:33:06 +00:00
James Bardin
371660ab8f cleanup panic output 2021-12-17 11:57:52 -05:00
James Bardin
583e3a5f0b use 11 for the panic exit code 2021-10-28 15:34:02 -04:00
James Bardin
bd37f43daa add note about calling PanicHandler first 2021-10-28 13:56:18 -04:00
James Bardin
d03a037567 insert panic handlers 2021-10-28 11:51:39 -04:00
James Bardin
3f31533f86 logging.PanicHandler can now be for internal use
Repurpose logging.PanicHandler to recover internal panics and print a
message to users similar to panicwrap.
2021-10-28 11:51:39 -04:00
James Bardin
d52e17e111 hide provider crashes from panicwrap when logging
When logging is turned on, panicwrap will still see provider crashes and
falsely report them as core crashes, hiding the formatted provider
error. We can trick panicwrap by slightly obfuscating the error line.
2020-11-05 10:54:21 -05:00
James Bardin
3225d9ac11 record all plugin panics, and print on main exit
Create a logger that will record any apparent crash output for later
processing.

If the cli command returns with a non-zero exit status, check for any
recorded crashes and add those to the output.
2020-10-26 09:34:03 -04:00
James Bardin
f8893785f0 separate core and provider loggers
Now that hclog can independently set levels on related loggers, we can
separate the log levels for different subsystems in terraform.

This adds the new environment variables, `TF_LOG_CORE` and
`TF_LOG_PROVIDER`, which each take the same set of log level arguments,
and only applies to logs from that subsystem. This means that setting
`TF_LOG_CORE=level` will not show logs from providers, and
`TF_LOG_PROVIDER=level` will not show logs from core. The behavior of
`TF_LOG` alone does not change.

While it is not necessarily needed since the default is to disable logs,
there is also a new level argument of `off`, which reflects the
associated level in hclog.
2020-10-23 12:46:32 -04:00
James Bardin
c2af5333e8 use a log sink to capture logs for panicwrap
Use a separate log sink to always capture trace logs for the panicwrap
handler to write out in a crash log.

This requires creating a log file in the outer process and passing that
path to the child process to log to.
2020-10-21 17:29:07 -04:00
James Bardin
b61488a8ba write traceback to log crash log 2020-10-21 17:29:07 -04:00
James Bardin
1d9d82973b move panicwrap handler to logging package 2020-10-21 13:47:16 -04:00
Renamed from panic.go (Browse further)