Skip to content

Generate LSP request IDs atomically #1162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2025
Merged

Conversation

jakebailey
Copy link
Member

WARNING: DATA RACE
Read at 0x00c000232a50 by goroutine 1006:
  github.com/microsoft/typescript-go/internal/lsp.(*Server).sendRequest()
      /home/jabaile/work/TypeScript-go/internal/lsp/server.go:402 +0x91
  github.com/microsoft/typescript-go/internal/lsp.(*Server).WatchFiles()
      /home/jabaile/work/TypeScript-go/internal/lsp/server.go:198 +0x36c
  github.com/microsoft/typescript-go/internal/project.(*watchedFiles[go.shape.[]string]).update()
      /home/jabaile/work/TypeScript-go/internal/project/watch.go:85 +0xbc1
  github.com/microsoft/typescript-go/internal/project.(*ConfigFileRegistry).updateRootFilesWatch()
      /home/jabaile/work/TypeScript-go/internal/project/configfileregistry.go:134 +0x990
  github.com/microsoft/typescript-go/internal/project.(*ConfigFileRegistry).AcquireConfig()
      /home/jabaile/work/TypeScript-go/internal/project/configfileregistry.go:111 +0x5c4
  github.com/microsoft/typescript-go/internal/project.(*Project).GetResolvedProjectReference()
      /home/jabaile/work/TypeScript-go/internal/project/project.go:290 +0x88
  github.com/microsoft/typescript-go/internal/compiler.(*projectReferenceParseTask).start()
      /home/jabaile/work/TypeScript-go/internal/compiler/projectreferenceparsetask.go:19 +0xde
  github.com/microsoft/typescript-go/internal/compiler.(*fileLoaderWorker[go.shape.*github.com/microsoft/typescript-go/internal/compiler.projectReferenceParseTask]).start.func1()
      /home/jabaile/work/TypeScript-go/internal/compiler/fileloadertask.go:35 +0x63
  github.com/microsoft/typescript-go/internal/core.(*parallelWorkGroup).Queue.func1()
      /home/jabaile/work/TypeScript-go/internal/core/workgroup.go:39 +0x8d

Previous write at 0x00c000232a50 by goroutine 1007:
  github.com/microsoft/typescript-go/internal/lsp.(*Server).sendRequest()
      /home/jabaile/work/TypeScript-go/internal/lsp/server.go:402 +0xad
  github.com/microsoft/typescript-go/internal/lsp.(*Server).WatchFiles()
      /home/jabaile/work/TypeScript-go/internal/lsp/server.go:198 +0x36c
  github.com/microsoft/typescript-go/internal/project.(*watchedFiles[go.shape.[]string]).update()
      /home/jabaile/work/TypeScript-go/internal/project/watch.go:85 +0xbc1
  github.com/microsoft/typescript-go/internal/project.(*ConfigFileRegistry).updateRootFilesWatch()
      /home/jabaile/work/TypeScript-go/internal/project/configfileregistry.go:134 +0x990
  github.com/microsoft/typescript-go/internal/project.(*ConfigFileRegistry).AcquireConfig()
      /home/jabaile/work/TypeScript-go/internal/project/configfileregistry.go:111 +0x5c4
  github.com/microsoft/typescript-go/internal/project.(*Project).GetResolvedProjectReference()
      /home/jabaile/work/TypeScript-go/internal/project/project.go:290 +0x88
  github.com/microsoft/typescript-go/internal/compiler.(*projectReferenceParseTask).start()
      /home/jabaile/work/TypeScript-go/internal/compiler/projectreferenceparsetask.go:19 +0xde
  github.com/microsoft/typescript-go/internal/compiler.(*fileLoaderWorker[go.shape.*github.com/microsoft/typescript-go/internal/compiler.projectReferenceParseTask]).start.func1()
      /home/jabaile/work/TypeScript-go/internal/compiler/fileloadertask.go:35 +0x63
  github.com/microsoft/typescript-go/internal/core.(*parallelWorkGroup).Queue.func1()
      /home/jabaile/work/TypeScript-go/internal/core/workgroup.go:39 +0x8d

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a data race on the LSP server’s request ID counter by switching to atomic increments.

  • Import sync/atomic and change clientSeq from int32 to atomic.Int32
  • Update sendRequest to use clientSeq.Add(1) when generating each new request ID
Comments suppressed due to low confidence (1)

internal/lsp/server.go:403

  • Add a concurrent test for sendRequest to ensure that request IDs remain unique and no data races occur when multiple goroutines generate IDs simultaneously.
id := lsproto.NewIDString(fmt.Sprintf("ts%d", s.clientSeq.Add(1)))

@jakebailey jakebailey added this pull request to the merge queue Jun 11, 2025
Merged via the queue into main with commit 61ebc5a Jun 11, 2025
22 checks passed
@jakebailey jakebailey deleted the jabaile/clientSeq-atomic branch June 11, 2025 04:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants