mattn / efm-langserver

General purpose Language Server
MIT License
1.36k stars 61 forks source link

Fix data race in text document formatting #242

Closed lotusirous closed 1 year ago

lotusirous commented 1 year ago

This pull request fixes a data race in text document formatting from the master 34b8f5bf9c80a361c56245131fa23b197f94b293

$ go test  ./... -race

?       github.com/mattn/efm-langserver [no test files]
[]
==================
WARNING: DATA RACE
Write at 0x00c0001823e0 by goroutine 10:
  github.com/mattn/efm-langserver/langserver.(*langHandler).formatRequest.func1()
      /Users/khant/temp/efm-langserver/langserver/handle_text_document_formatting.go:41 +0x3e

Previous write at 0x00c0001823e0 by goroutine 8:
  github.com/mattn/efm-langserver/langserver.(*langHandler).formatRequest()
      /Users/khant/temp/efm-langserver/langserver/handle_text_document_formatting.go:40 +0x1cd
  github.com/mattn/efm-langserver/langserver.TestFormattingRequireRootMatcher()
      /Users/khant/temp/efm-langserver/langserver/handle_text_document_formatting_test.go:37 +0x6ad
  testing.tRunner()
      /Users/khant/local/go/src/testing/testing.go:1576 +0x216
  testing.(*T).Run.func1()
      /Users/khant/local/go/src/testing/testing.go:1629 +0x47

Goroutine 10 (running) created at:
  time.goFunc()
      /Users/khant/local/go/src/time/sleep.go:176 +0x47

Goroutine 8 (finished) created at:
  testing.(*T).Run()
      /Users/khant/local/go/src/testing/testing.go:1629 +0x805
  testing.runTests.func1()
      /Users/khant/local/go/src/testing/testing.go:2036 +0x8d
  testing.tRunner()
      /Users/khant/local/go/src/testing/testing.go:1576 +0x216
  testing.runTests()
      /Users/khant/local/go/src/testing/testing.go:2034 +0x87c
  testing.(*M).Run()
      /Users/khant/local/go/src/testing/testing.go:1906 +0xb44
  main.main()
      _testmain.go:77 +0x2e9
==================
FAIL
FAIL    github.com/mattn/efm-langserver/langserver  0.784s
FAIL
lotusirous commented 1 year ago

The current golangci-lint being used in the GitHub action appears to be outdated. Please refer to the log for versions 1.50.1 and 1.52.2.

$ golangci-lint-1.50.1 run --out-format=github-actions --verbose

INFO [config_reader] Config search paths: [./ /Users/khant/local/lsp/efm-langserver /Users/khant/local/lsp /Users/khant/local /Users/khant /Users /]
INFO [config_reader] Used config file .golangci.yaml
INFO [lintersdb] Active 4 linters: [goimports govet revive typecheck]
INFO [loader] Go packages loading at mode 575 (types_sizes|compiled_files|deps|exports_file|imports|files|name) took 169.292813ms
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 4.750424ms
INFO [linters_context/goanalysis] analyzers took 6.273071ms with top 10 stages: printf: 2.761186ms, ctrlflow: 1.681157ms, inspect: 649.216µs, lostcancel: 552.287µs, loopclosure: 454.091µs, framepointer: 34.668µs, goimports: 26.834µs, httpresponse: 26.021µs, asmdecl: 19.82µs, errorsas: 8.793µs
INFO [runner/max_same_issues] 39/42 issues with text "e.WriteByte undefined (type *encodeState has no field or method WriteByte)" were hidden, use --max-same-issues
INFO [runner/max_same_issues] 25/28 issues with text "e.WriteString undefined (type *encodeState has no field or method WriteString)" were hidden, use --max-same-issues
INFO [runner/max_same_issues] 18/21 issues with text "inst.Out undefined (type *onePassInst has no field or method Out)" were hidden, use --max-same-issues
INFO [runner/max_same_issues] 10/13 issues with text "String not declared by package unsafe" were hidden, use --max-same-issues
INFO [runner/max_same_issues] 6/9 issues with text "e.Write undefined (type *encodeState has no field or method Write)" were hidden, use --max-same-issues
INFO [runner/max_same_issues] 4/7 issues with text "inst.Arg undefined (type *onePassInst has no field or method Arg)" were hidden, use --max-same-issues
INFO [runner/max_same_issues] 4/7 issues with text "inst.Rune undefined (type *onePassInst has no field or method Rune)" were hidden, use --max-same-issues
INFO [runner/max_same_issues] 4/7 issues with text "StringData not declared by package unsafe" were hidden, use --max-same-issues
INFO [runner/max_same_issues] 3/6 issues with text "inst.Op undefined (type *onePassInst has no field or method Op)" were hidden, use --max-same-issues
INFO [runner/max_same_issues] 3/6 issues with text "undeclared name: `unicodeclass`" were hidden, use --max-same-issues
INFO [runner/max_from_linter] 30/80 issues from linter typecheck were hidden, use --max-issues-per-linter
INFO [runner] Issues before processing: 912, after processing: 50
INFO [runner] Processors filtering stat (out/in): filename_unadjuster: 912/912, autogenerated_exclude: 912/912, exclude: 912/912, exclude-rules: 912/912, source_code: 50/50, severity-rules: 50/50, cgo: 912/912, path_prettifier: 912/912, identifier_marker: 912/912, uniq_by_line: 196/912, max_same_issues: 80/196, max_from_linter: 50/80, path_shortener: 50/50, sort_results: 50/50, skip_dirs: 912/912, nolint: 912/912, max_per_file_from_linter: 196/196, skip_files: 912/912, diff: 196/196, path_prefixer: 50/50
INFO [runner] processing took 60.140917ms with stages: nolint: 24.447643ms, exclude-rules: 23.189473ms, identifier_marker: 9.606269ms, path_prettifier: 1.421838ms, source_code: 757.445µs, skip_dirs: 283.769µs, max_same_issues: 203.449µs, uniq_by_line: 106.168µs, cgo: 47.301µs, filename_unadjuster: 26.37µs, autogenerated_exclude: 24.352µs, max_from_linter: 8.325µs, max_per_file_from_linter: 7.602µs, path_shortener: 7.161µs, skip_files: 2.483µs, sort_results: 338ns, diff: 289ns, exclude: 226ns, severity-rules: 224ns, path_prefixer: 192ns
INFO [runner] linters took 468.124442ms with stages: goanalysis_metalinter: 407.845342ms
::error file=main.go,line=77,col=9::undeclared name: `yaml` (typecheck)
::error file=langserver/config.go,line=28,col=8::undeclared name: `yaml` (typecheck)
::error file=langserver/config.go,line=35,col=9::undeclared name: `yaml` (typecheck)
::error file=langserver/handle_text_document_definition.go,line=143,col=13::undeclared name: `unicodeclass` (typecheck)
::error file=langserver/handle_text_document_definition.go,line=145,col=14::undeclared name: `unicodeclass` (typecheck)
::error file=langserver/handle_text_document_hover.go,line=82,col=14::undeclared name: `unicodeclass` (typecheck)
::error file=../../go/src/internal/reflectlite/type.go,line=346,col=16::String not declared by package unsafe (typecheck)
::error file=../../go/src/internal/reflectlite/type.go,line=355,col=16::String not declared by package unsafe (typecheck)
::error file=../../go/src/runtime/debuglog.go,line=296,col=20::StringData not declared by package unsafe (typecheck)
::error file=../../go/src/runtime/heapdump.go,line=159,col=37::StringData not declared by package unsafe (typecheck)
::error file=../../go/src/runtime/heapdump.go,line=202,col=32::StringData not declared by package unsafe (typecheck)
::error file=../../go/src/runtime/proc.go,line=658,col=16::String not declared by package unsafe (typecheck)
::error file=../../go/src/syscall/syscall_unix.go,line=63,col=4::m.Lock undefined (type *mmapper has no field or method Lock) (typecheck)
::error file=../../go/src/syscall/syscall_unix.go,line=64,col=10::m.Unlock undefined (type *mmapper has no field or method Unlock) (typecheck)
::error file=../../go/src/syscall/syscall_unix.go,line=76,col=4::m.Lock undefined (type *mmapper has no field or method Lock) (typecheck)
::error file=../../go/src/syscall/syscall_unix.go,line=77,col=10::m.Unlock undefined (type *mmapper has no field or method Unlock) (typecheck)
::error file=../../go/src/flag/flag.go,line=421,col=17::invalid operation: cannot compare f.output == nil (operator == not defined on untyped nil) (typecheck)
::error file=../../go/src/internal/fmtsort/sort.go,line=182,col=1::missing return (typecheck)
::error file=../../go/src/io/pipe.go,line=22,col=4::a.Lock undefined (type *onceError has no field or method Lock) (typecheck)
::error file=../../go/src/io/pipe.go,line=23,col=10::a.Unlock undefined (type *onceError has no field or method Unlock) (typecheck)
::error file=../../go/src/io/pipe.go,line=30,col=4::a.Lock undefined (type *onceError has no field or method Lock) (typecheck)
::error file=../../go/src/io/pipe.go,line=31,col=10::a.Unlock undefined (type *onceError has no field or method Unlock) (typecheck)
::error file=../../go/src/os/file_unix.go,line=416,col=15::invalid operation: cannot compare d.info != nil (operator != not defined on untyped nil) (typecheck)
::error file=../../go/src/encoding/json/decode.go,line=161,col=15::invalid operation: cannot compare e.Type == nil (operator == not defined on untyped nil) (typecheck)
::error file=../../go/src/encoding/json/decode.go,line=254,col=56::invalid operation: cannot compare d.errorContext.Struct != nil (operator != not defined on untyped nil) (typecheck)
::error file=../../go/src/encoding/json/encode.go,line=165,col=31::e.Bytes undefined (type *encodeState has no field or method Bytes) (typecheck)
::error file=../../go/src/encoding/json/encode.go,line=464,col=4::e.WriteString undefined (type *encodeState has no field or method WriteString) (typecheck)
::error file=../../go/src/encoding/json/encode.go,line=469,col=5::e.WriteString undefined (type *encodeState has no field or method WriteString) (typecheck)
::error file=../../go/src/encoding/json/encode.go,line=474,col=5::e.WriteString undefined (type *encodeState has no field or method WriteString) (typecheck)
::error file=../../go/src/encoding/json/encode.go,line=537,col=5::e.WriteByte undefined (type *encodeState has no field or method WriteByte) (typecheck)
::error file=../../go/src/encoding/json/encode.go,line=545,col=5::e.WriteByte undefined (type *encodeState has no field or method WriteByte) (typecheck)
::error file=../../go/src/encoding/json/encode.go,line=552,col=5::e.WriteByte undefined (type *encodeState has no field or method WriteByte) (typecheck)
::error file=../../go/src/encoding/json/encode.go,line=554,col=4::e.Write undefined (type *encodeState has no field or method Write) (typecheck)
::error file=../../go/src/encoding/json/encode.go,line=565,col=4::e.Write undefined (type *encodeState has no field or method Write) (typecheck)
::error file=../../go/src/encoding/json/encode.go,line=606,col=4::e.Write undefined (type *encodeState has no field or method Write) (typecheck)
::error file=../../go/src/encoding/json/encode.go,line=642,col=20::e2.Bytes undefined (type *encodeState has no field or method Bytes) (typecheck)
::error file=../../go/src/encoding/json/stream.go,line=222,col=9::e.Bytes undefined (type *encodeState has no field or method Bytes) (typecheck)
::error file=../../go/src/regexp/exec.go,line=78,col=26::invalid operation: cannot compare i.reader.r != nil (operator != not defined on untyped nil) (typecheck)
::error file=../../go/src/regexp/exec.go,line=432,col=48::inst.Arg undefined (type *onePassInst has no field or method Arg) (typecheck)
::error file=../../go/src/regexp/exec.go,line=446,col=17::inst.Out undefined (type *onePassInst has no field or method Out) (typecheck)
::error file=../../go/src/regexp/exec.go,line=447,col=15::inst.Op undefined (type *onePassInst has no field or method Op) (typecheck)
::error file=../../go/src/regexp/exec.go,line=458,col=13::inst.MatchRune undefined (type *onePassInst has no field or method MatchRune) (typecheck)
::error file=../../go/src/regexp/exec.go,line=462,col=17::inst.Rune undefined (type *onePassInst has no field or method Rune) (typecheck)
::error file=../../go/src/regexp/exec.go,line=480,col=39::inst.Arg undefined (type *onePassInst has no field or method Arg) (typecheck)
::error file=../../go/src/regexp/exec.go,line=485,col=16::inst.Arg undefined (type *onePassInst has no field or method Arg) (typecheck)
::error file=../../go/src/regexp/onepass.go,line=76,col=12::i.MatchRunePos undefined (type *onePassInst has no field or method MatchRunePos) (typecheck)
::error file=../../go/src/regexp/onepass.go,line=80,col=7::i.Op undefined (type *onePassInst has no field or method Op) (typecheck)
::error file=../../go/src/regexp/onepass.go,line=81,col=12::i.Out undefined (type *onePassInst has no field or method Out) (typecheck)
::error file=../../go/src/regexp/onepass.go,line=238,col=21::p.Inst[pc].Op undefined (type onePassInst has no field or method Op) (typecheck)
::error file=../../go/src/regexp/onepass.go,line=243,col=29::p.Inst[pc].Out undefined (type onePassInst has no field or method Out) (typecheck)
INFO File cache stats: 18 entries of total size 439.7KiB
INFO Memory: 8 samples, avg is 71.4MB, max is 109.7MB
INFO Execution took 655.39675ms

The below log is from version v1.52.2

$ golangci-lint-1.52.2 run --out-format=github-actions --verbose

INFO [config_reader] Config search paths: [./ /Users/khant/local/lsp/efm-langserver /Users/khant/local/lsp /Users/khant/local /Users/khant /Users /]
INFO [config_reader] Used config file .golangci.yaml
INFO [lintersdb] Active 4 linters: [goimports govet revive typecheck]
INFO [loader] Go packages loading at mode 575 (types_sizes|files|imports|name|compiled_files|deps|exports_file) took 363.957484ms
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 5.523155ms
INFO [linters_context/goanalysis] analyzers took 0s with no stages
INFO [runner] Issues before processing: 13, after processing: 0
INFO [runner] Processors filtering stat (out/in): filename_unadjuster: 13/13, identifier_marker: 13/13, exclude-rules: 0/13, cgo: 13/13, skip_dirs: 13/13, autogenerated_exclude: 13/13, exclude: 13/13, path_prettifier: 13/13, skip_files: 13/13
INFO [runner] processing took 1.11243ms with stages: path_prettifier: 654.871µs, identifier_marker: 286.975µs, autogenerated_exclude: 107.987µs, skip_dirs: 32.305µs, exclude-rules: 18.598µs, cgo: 3.696µs, fixer: 2.259µs, filename_unadjuster: 982ns, max_same_issues: 943ns, skip_files: 818ns, nolint: 813ns, source_code: 359ns, uniq_by_line: 294ns, exclude: 287ns, max_from_linter: 287ns, severity-rules: 214ns, diff: 204ns, sort_results: 165ns, path_shortener: 138ns, max_per_file_from_linter: 124ns, path_prefixer: 111ns
INFO [runner] linters took 33.639734ms with stages: goanalysis_metalinter: 32.447745ms
INFO File cache stats: 0 entries of total size 0B
INFO Memory: 6 samples, avg is 35.2MB, max is 40.9MB
INFO Execution took 416.155722ms
mattn commented 1 year ago

Thank you