When main.main returns, the process exits, so there's no need to cancel contexts.
This change was initially reviewed as
https://go-review.googlesource.com/c/go/+/106915/4
but somehow I messed up and committed patchset 5, which was
effectively empty.
Change-Id: Ic4250eb6563af9bc734e429aafc7081ca7d0e012
Reviewed-on: https://go-review.googlesource.com/c/148758
Reviewed-by: Alan Donovan <adonovan@google.com>
var sig *types.Signature
switch node := node.(type) {
case *ast.FuncDecl:
+ if node.Name.Name == "main" && node.Recv == nil && f.file.Name.Name == "main" {
+ // Returning from main.main terminates the process,
+ // so there's no need to cancel contexts.
+ return
+ }
obj := f.pkg.defs[node.Name]
if obj == nil {
return // type error (e.g. duplicate function declaration)
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package testdata
+package main
import (
"context"
ctx, _ = context.WithDeadline() // ERROR "the cancel function returned by context.WithDeadline should be called, not discarded, to avoid a context leak"
}
+// Return from main is handled specially.
+// Since the program exits, there's no need to call cancel.
+func main() {
+ var ctx, cancel = context.WithCancel()
+}
+
func _() {
ctx, cancel := context.WithCancel()
defer cancel() // ok