Skip to content
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

empty-block: Fix false positive on select {} #805

Merged
merged 1 commit into from
Mar 16, 2023

Conversation

abhinav
Copy link
Contributor

@abhinav abhinav commented Mar 15, 2023

This fixes a false positive reported by revive on the following:

select {}

This is a way to block the program indefinitely.
It is used in cases like:

  • Running a long-running server in a background thread
    and blocking main from exiting until the application dies.
    This is something you might do if your process has
    multiple servers/listeners in the same process.

    go grpcListenAndServe()
    go httpListenAndServe()
    go logFlusher()
    
    select {}
  • In programs compiled to WASM to prevent the application from exiting,
    so that the Javascript components may interact with it.

    func main() {
      js.Global().Set("foo", foo)
      select {}
    }

    Without this, one may see an error like,
    "Error: Go program has already exited"

As a workaround, these programs can block forever
by receiving from a throwaway channel (<-make(chan struct{})),
but select {} is still a completely valid way of doing this,
so supporting it makes sense.

The issue was previously reported in #698 but was closed
because the author was satisfied with a //nolint comment.

Now that this rule is part of the default rule set (#799)
the case for addressing the false positive is stronger.

Resolves #804

This fixes a false positive reported by revive on the following:

    select {}

This is a way to block the program indefinitely.
It is used in cases like:

- Running a long-running server in a background thread
  and blocking `main` from exiting until the application dies.
  This is something you might do if your process has
  multiple servers/listeners in the same process.

  ```go
  go grpcListenAndServe()
  go httpListenAndServe()
  go logFlusher()

  select {}
  ```

- In programs compiled to WASM to prevent the application from exiting,
  so that the Javascript components may interact with it.

  ```go
  func main() {
    js.Global().Set("foo", foo)
    select {}
  }
  ```

  Without this, one may see an error like,
  "Error: Go program has already exited"

As a workaround, these programs can block forever
by receiving from a throwaway channel (`<-make(chan struct{})`),
but `select {}` is still a completely valid way of doing this,
so supporting it makes sense.

The issue was previously reported in mgechev#698 but was closed
because the author was satisfied with a `//nolint` comment.

Now that this rule is part of the default rule set (mgechev#799)
the case for addressing the false positive is stronger.

Resolves mgechev#804
@mgechev mgechev merged commit b03e54f into mgechev:master Mar 16, 2023
@abhinav abhinav deleted the empty-select branch March 16, 2023 20:51
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.

False positive on select {}
2 participants