Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions private/buf/buflsp/buflsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@
logger.Info("starting LSP server")

conn := jsonrpc2.NewConn(stream)
// connCtx is a context scoped to the connection's lifetime. It is cancelled
// when the connection is done (or when ctx is cancelled), so that background
// goroutines (e.g. RunChecks) do not outlive the connection.
connCtx, connCancel := context.WithCancel(context.Background())
go func() {
select {
case <-ctx.Done():
case <-conn.Done():
}
connCancel()
}()
lsp := &lsp{
conn: conn,
client: protocol.ClientDispatcher(
Expand All @@ -76,6 +87,7 @@
queryExecutor: queryExecutor,
opener: source.NewMap(nil),
irSession: new(ir.Session),
connCtx: connCtx,
}
lsp.fileManager = newFileManager(lsp)
lsp.workspaceManager = newWorkspaceManager(lsp)
Expand Down Expand Up @@ -103,6 +115,7 @@
conn jsonrpc2.Conn
client protocol.Client
container appext.Container
connCtx context.Context // cancelled when the connection is done

Check failure on line 118 in private/buf/buflsp/buflsp.go

View workflow job for this annotation

GitHub Actions / lint

found a struct that contains a context.Context field (containedctx)

logger *slog.Logger
bufVersion string // buf version, set at server creation
Expand Down
4 changes: 2 additions & 2 deletions private/buf/buflsp/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ func (f *file) RunChecks(ctx context.Context) {
}

const checkTimeout = 30 * time.Second
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), checkTimeout)
ctx, cancel := context.WithTimeout(f.lsp.connCtx, checkTimeout)
f.cancelChecks = cancel

go func() {
Expand All @@ -1079,7 +1079,7 @@ func (f *file) RunChecks(ctx context.Context) {
); err != nil {
var fileAnnotationSet bufanalysis.FileAnnotationSet
if !errors.As(err, &fileAnnotationSet) {
if errors.Is(err, context.Canceled) {
if errors.Is(err, context.Canceled) || ctx.Err() != nil {
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.

related to this: pluginrpc/pluginrpc-go#17

f.lsp.logger.DebugContext(ctx, "checks cancelled", slog.String("uri", f.uri.Filename()), xslog.ErrorAttr(err))
} else if errors.Is(err, context.DeadlineExceeded) {
f.lsp.logger.WarnContext(ctx, "checks deadline exceeded", slog.String("uri", f.uri.Filename()), xslog.ErrorAttr(err))
Expand Down
2 changes: 1 addition & 1 deletion private/buf/buflsp/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func buildImage(
var diagnostics []protocol.Diagnostic
compiled, err := compiler.Compile(ctx, path)
if err != nil {
logger.Warn("error building image", slog.String("path", path), xslog.ErrorAttr(err))
logger.WarnContext(ctx, "error building image", slog.String("path", path), xslog.ErrorAttr(err))
var errorWithPos reporter.ErrorWithPos
if errors.As(err, &errorWithPos) {
diagnostics = []protocol.Diagnostic{newDiagnostic(errorWithPos, false, opener, logger)}
Expand Down
Loading