Repository: uber-go/fx
Pull request #727 (2021-02-22 20:32:07)

Use x/sys instead of syscall (fixes uber-go/fx#682)
This removes the use of package syscall in favor of x/sys/{unix,windows}.

Please note that I've removed the explicit check for SIGTERM in shutdown_test.go, and replaced it with a non-nil check. In theory you could explicitly check for the signal as had been done before, but since Shutdown's documentation only mentions "a signal" being sent, I don't see it necessary to check for an implementation detail.

codecov[bot]: # [Codecov](https://codecov.io/gh/uber-go/fx/pull/727?src=pr&el=h1) Report
> Merging [#727](https://codecov.io/gh/uber-go/fx/pull/727?src=pr&el=desc) (2ba5d00) into [master](https://codecov.io/gh/uber-go/fx/commit/b143fbfecdfdbffdd804a139ae63d4ab0c9bbbaf?el=desc) (b143fbf) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/uber-go/fx/pull/727/graphs/tree.svg?width=650&height=150&src=pr&token=PYY9PSfaC6)](https://codecov.io/gh/uber-go/fx/pull/727?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master     #727   +/-   ##
=======================================
  Coverage   97.66%   97.66%           
=======================================
  Files          19       19           
  Lines         513      513           
=======================================
  Hits          501      501           
  Misses         10       10           
  Partials        2        2           
```


| [Impacted Files](https://codecov.io/gh/uber-go/fx/pull/727?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [app.go](https://codecov.io/gh/uber-go/fx/pull/727/diff?src=pr&el=tree#diff-YXBwLmdv) | `95.50% <100.00%> (ø)` | |
| [shutdown.go](https://codecov.io/gh/uber-go/fx/pull/727/diff?src=pr&el=tree#diff-c2h1dGRvd24uZ28=) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/uber-go/fx/pull/727?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/uber-go/fx/pull/727?src=pr&el=footer). Last update [b143fbf...2ba5d00](https://codecov.io/gh/uber-go/fx/pull/727?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

blizzy78: Done and done. I had added the license headers locally as suggested by `make lint`, then forgot to push 🙈
blizzy78: I believe [this Travis CI run](https://travis-ci.com/github/uber-go/fx/jobs/485183856) should have failed earlier due to missing license headers? It looks like it did not.
abhinav: > I believe [this Travis CI run](https://travis-ci.com/github/uber-go/fx/jobs/485183856) should have failed earlier due to missing license headers? It looks like it did not.

Weirdly, the oldstable run failed instead. That appears to be a mistake; fixing.

Patch:
diff --git a/app.go b/app.go
index b5865d2..e1e25de 100644
--- a/app.go
+++ b/app.go
@@ -30,7 +30,6 @@ import (
 	"reflect"
 	"strings"
 	"sync"
-	"syscall"
 	"time"
 
 	"go.uber.org/dig"
@@ -605,7 +604,7 @@ func (app *App) Stop(ctx context.Context) error {
 // using the Shutdown functionality (see the Shutdowner documentation for details).
 func (app *App) Done() <-chan os.Signal {
 	c := make(chan os.Signal, 1)
-	signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
+	signal.Notify(c, _sigINT, _sigTERM)
 
 	app.donesMu.Lock()
 	app.dones = append(app.dones, c)
diff --git a/app_unixes.go b/app_unixes.go
new file mode 100644
index 0000000..80b6689
--- /dev/null
+++ b/app_unixes.go
@@ -0,0 +1,28 @@
+// Copyright (c) 2021 Uber Technologies, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
+
+package fx
+
+import "golang.org/x/sys/unix"
+
+const _sigINT = unix.SIGINT
+const _sigTERM = unix.SIGTERM
diff --git a/app_windows.go b/app_windows.go
new file mode 100644
index 0000000..b5cab9f
--- /dev/null
+++ b/app_windows.go
@@ -0,0 +1,28 @@
+// Copyright (c) 2021 Uber Technologies, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+// +build windows
+
+package fx
+
+import "golang.org/x/sys/windows"
+
+const _sigINT = windows.SIGINT
+const _sigTERM = windows.SIGTERM
diff --git a/go.mod b/go.mod
index ef37ef1..af4c959 100644
--- a/go.mod
+++ b/go.mod
@@ -9,5 +9,6 @@ require (
 	go.uber.org/multierr v1.5.0
 	go.uber.org/zap v1.16.0
 	golang.org/x/lint v0.0.0-20190930215403-16217165b5de
+	golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43
 	golang.org/x/tools v0.0.0-20191114200427-caa0b0f7d508 // indirect
 )
diff --git a/go.sum b/go.sum
index 17358f8..caafc8c 100644
--- a/go.sum
+++ b/go.sum
@@ -1,6 +1,5 @@
 github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
-github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -11,6 +10,7 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
 github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
 github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
 github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
+github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
 github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -19,16 +19,12 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
 github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
 github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
 github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
-go.uber.org/atomic v1.5.0 h1:OI5t8sDa1Or+q8AeE+yKeB/SDYioSHAgcVljj9JIETY=
-go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
 go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=
 go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
 go.uber.org/dig v1.10.0 h1:yLmDDj9/zuDjv3gz8GQGviXMs9TfysIUMUilCpgzUJY=
 go.uber.org/dig v1.10.0/go.mod h1:X34SnWGr8Fyla9zQNO2GSO2D+TIuqB14OS8JhYocIyw=
 go.uber.org/goleak v0.10.0 h1:G3eWbSNIskeRqtsN/1uI5B+eP73y3JUuBsv9AZjehb4=
 go.uber.org/goleak v0.10.0/go.mod h1:VCZuO8V8mFPlL0F5J5GK1rtHV3DrFcQ1R8ryq7FK0aI=
-go.uber.org/multierr v1.4.0 h1:f3WCSC2KzAcBXGATIxAB1E2XuCpNU255wNKZ505qi3E=
-go.uber.org/multierr v1.4.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
 go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A=
 go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
 go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee h1:0mgffUl7nfd+FpvXMVz4IDEaUSmT1ysygQC7qYo7sG4=
@@ -46,8 +42,9 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
 golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43 h1:SgQ6LNaYJU0JIuEHv9+s6EbhSCwYeAf5Yvj6lpYlqAE=
+golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
-golang.org/x/tools v0.0.0-20190311212946-11955173bddd h1:/e+gpKk9r3dJobndpTytxS2gOy6m5uvpg+ISQoEcusQ=
 golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
 golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
 golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
@@ -56,7 +53,6 @@ golang.org/x/tools v0.0.0-20191030062658-86caa796c7ab/go.mod h1:b+2E5dAYhXwXZwtn
 golang.org/x/tools v0.0.0-20191114200427-caa0b0f7d508 h1:0FYNp0PF9kFm/ZUrvcJiQ12IUJJG7iAc6Cu01wbKrbU=
 golang.org/x/tools v0.0.0-20191114200427-caa0b0f7d508/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
 golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
-gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
diff --git a/shutdown.go b/shutdown.go
index 928e45d..68c5a7f 100644
--- a/shutdown.go
+++ b/shutdown.go
@@ -23,7 +23,6 @@ package fx
 import (
 	"fmt"
 	"os"
-	"syscall"
 )
 
 // Shutdowner provides a method that can manually trigger the shutdown of the
@@ -47,7 +46,7 @@ type shutdowner struct {
 // Shutdown broadcasts a signal to all of the application's Done channels
 // and begins the Stop process.
 func (s *shutdowner) Shutdown(opts ...ShutdownOption) error {
-	return s.app.broadcastSignal(syscall.SIGTERM)
+	return s.app.broadcastSignal(_sigTERM)
 }
 
 func (app *App) shutdowner() Shutdowner {


Test patch:
diff --git a/app_internal_test.go b/app_internal_test.go
index 55e0836..df903af 100644
--- a/app_internal_test.go
+++ b/app_internal_test.go
@@ -24,7 +24,6 @@ import (
 	"fmt"
 	"os"
 	"sync"
-	"syscall"
 	"testing"
 
 	"github.com/stretchr/testify/assert"
@@ -42,7 +41,7 @@ func TestAppRun(t *testing.T) {
 		app.run(done)
 	}()
 
-	done <- syscall.SIGINT
+	done <- _sigINT
 	wg.Wait()
 }
 
diff --git a/shutdown_test.go b/shutdown_test.go
index 71eeeb5..7b29e18 100644
--- a/shutdown_test.go
+++ b/shutdown_test.go
@@ -21,7 +21,6 @@
 package fx_test
 
 import (
-	"syscall"
 	"testing"
 
 	"github.com/stretchr/testify/assert"
@@ -41,8 +40,8 @@ func TestShutdown(t *testing.T) {
 		defer app.RequireStart().RequireStop()
 
 		assert.NoError(t, s.Shutdown(), "error in app shutdown")
-		assert.Equal(t, syscall.SIGTERM, <-done1, "done channel 1 did not receive signal")
-		assert.Equal(t, syscall.SIGTERM, <-done2, "done channel 2 did not receive signal")
+		assert.NotNil(t, <-done1, "done channel 1 did not receive signal")
+		assert.NotNil(t, <-done2, "done channel 2 did not receive signal")
 	})
 
 	t.Run("ErrorOnUnsentSignal", func(t *testing.T) {
@@ -58,6 +57,6 @@ func TestShutdown(t *testing.T) {
 
 		assert.EqualError(t, s.Shutdown(), "failed to send terminated signal to 1 out of 1 channels",
 			"unexpected error returned when shutdown is called with a blocked channel")
-		assert.Equal(t, syscall.SIGTERM, <-done, "done channel did not receive signal")
+		assert.NotNil(t, <-done, "done channel did not receive signal")
 	})
 }