Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

fix/sg: fix mangled log output from sg start and sg run#63405

Merged
bobheadxi merged 3 commits into
mainfrom
sg-fix-mangled-output
Jun 20, 2024
Merged

fix/sg: fix mangled log output from sg start and sg run#63405
bobheadxi merged 3 commits into
mainfrom
sg-fix-mangled-output

Conversation

@bobheadxi

@bobheadxi bobheadxi commented Jun 20, 2024

Copy link
Copy Markdown
Member

Right now sg run / sg start can horribly mangle multi-line output. A nicely annotated report from @unknwon:

image

Replacing the "buffered process logger" thing with https://github.com/bobheadxi/streamline which powers sourcegraph/run etc (fairly reliably if I do say so myself) fixes this for a few cases where I can reliably repro wonky misordered output 😁

Test plan

sg start dotcom with sg.config.overwrite.yaml:

commands:
  enterprise-portal:
    env:
      SRC_LOG_LEVEL: debug
      PG_QUERY_LOGGING: true

Log scope pgx.devtracer is consistently formatted ✅ , even with high volume of logs

image

Also don't see anything suspicious happening after running for a while

@cla-bot cla-bot Bot added the cla-signed label Jun 20, 2024
@bobheadxi bobheadxi requested review from a team and unknwon June 20, 2024 21:22
@bobheadxi bobheadxi changed the title fix/sg: fix mangled log output fix/sg: fix mangled log output from sg start and sg run Jun 20, 2024
<-start
err := stream.Stream(func(line string) {
mux.Lock()
out.Writef(lineFormat, output.StyleBold, color, name, output.StyleReset, line)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*output.Output is not concurrency-safe

@unknwon

unknwon commented Jun 20, 2024

Copy link
Copy Markdown
Contributor

CleanShot 2024-06-20 at 19 01 12@2x

Seems fixed it!

@bobheadxi bobheadxi merged commit 78dcd57 into main Jun 20, 2024
@bobheadxi bobheadxi deleted the sg-fix-mangled-output branch June 20, 2024 23:07
@jhchabran

Copy link
Copy Markdown
Contributor

Thanks!

jhchabran referenced this pull request Jul 4, 2024
Fixes DINF-82; This was very much a rabbithole. A few things: 

- The race that @bobheadxi mentioned here
https://github.com/sourcegraph/sourcegraph/pull/63405#discussion_r1648180713
wasn't from `*output.Output` being unsafe, but `outputtest.Buffer` as it
happened again (see
[DINF-82](https://linear.app/sourcegraph/issue/DINF-82/devsgsg-test-failed-with-a-detected-race-condition))
- There something messed up with `cmds.start()`, which sometimes ends up
printing the command output _after_ the exit message instead of before.
- The crude `sort.Strings(want|have)` that was there already fixes that.
- And without the sleep, it's possible to read the output from the
`outputtest.Buffer` before the command outputs get written to it.
- The `time.Sleep(300 * time.Milliseconds)` _mitigates/hides_ that
problem.

At least, this shouldn't blow up in CI and buys us time to fix the whole
thing. We're tracking this in DINF-104. And out of 200 runs, I also
stumbled on a race in `progress_tty`, tracked in DINF-105 (that packages
is originally meant to be used by `src-cli` and was re-used for `sg` 3
years ago).

I'm pretty unhappy about the solution, but a bandage is better than
nothing. While ideally, we should really reconsider dropping
`std.Output` entirely in `sg` and use the good stuff from
github.com/charmbracelet instead because we don't want to spend too much
time on arcane terminal things ourselves, I'm much more about concerned
the concurrency issues mentioned above.

## Test plan

CI + `sg bazel test //dev/sg:sg_test --runs_per_test=100`
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants