Skip to content

Commit caa70e3

Browse files
Avoid exiting with os.Exit (#2434)
Avoid exiting with os.Exit (#2434)
1 parent 3a7d1d2 commit caa70e3

File tree

7 files changed

+60
-44
lines changed

7 files changed

+60
-44
lines changed

NOTICE.txt

+8-8
Original file line numberDiff line numberDiff line change
@@ -1363,11 +1363,11 @@ SOFTWARE
13631363

13641364
--------------------------------------------------------------------------------
13651365
Dependency : github.com/elastic/elastic-agent-libs
1366-
Version: v0.2.16
1366+
Version: v0.3.6
13671367
Licence type (autodetected): Apache-2.0
13681368
--------------------------------------------------------------------------------
13691369

1370-
Contents of probable licence file $GOMODCACHE/github.com/elastic/elastic-agent-libs@v0.2.16/LICENSE:
1370+
Contents of probable licence file $GOMODCACHE/github.com/elastic/elastic-agent-libs@v0.3.6/LICENSE:
13711371

13721372
Apache License
13731373
Version 2.0, January 2004
@@ -3630,11 +3630,11 @@ Exhibit B - "Incompatible With Secondary Licenses" Notice
36303630

36313631
--------------------------------------------------------------------------------
36323632
Dependency : github.com/mitchellh/hashstructure
3633-
Version: v0.0.0-20170116052023-ab25296c0f51
3633+
Version: v1.1.0
36343634
Licence type (autodetected): MIT
36353635
--------------------------------------------------------------------------------
36363636

3637-
Contents of probable licence file $GOMODCACHE/github.com/mitchellh/hashstructure@v0.0.0-20170116052023-ab25296c0f51/LICENSE:
3637+
Contents of probable licence file $GOMODCACHE/github.com/mitchellh/hashstructure@v1.1.0/LICENSE:
36383638

36393639
The MIT License (MIT)
36403640

@@ -4299,11 +4299,11 @@ Contents of probable licence file $GOMODCACHE/github.com/spf13/cobra@v1.3.0/LICE
42994299

43004300
--------------------------------------------------------------------------------
43014301
Dependency : github.com/stretchr/testify
4302-
Version: v1.7.0
4302+
Version: v1.8.2
43034303
Licence type (autodetected): MIT
43044304
--------------------------------------------------------------------------------
43054305

4306-
Contents of probable licence file $GOMODCACHE/github.com/stretchr/testify@v1.7.0/LICENSE:
4306+
Contents of probable licence file $GOMODCACHE/github.com/stretchr/testify@v1.8.2/LICENSE:
43074307

43084308
MIT License
43094309

@@ -14253,11 +14253,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1425314253

1425414254
--------------------------------------------------------------------------------
1425514255
Dependency : github.com/stretchr/objx
14256-
Version: v0.2.0
14256+
Version: v0.5.0
1425714257
Licence type (autodetected): MIT
1425814258
--------------------------------------------------------------------------------
1425914259

14260-
Contents of probable licence file $GOMODCACHE/github.com/stretchr/objx@v0.2.0/LICENSE:
14260+
Contents of probable licence file $GOMODCACHE/github.com/stretchr/objx@v0.5.0/LICENSE:
1426114261

1426214262
The MIT License
1426314263

dev-tools/mage/gotest_test.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -193,48 +193,48 @@ func TestGoTest_Helper_AssertOutput(t *testing.T) {
193193
var wantTestAssertOutput = `(?sm:
194194
=== Failed
195195
=== FAIL: dev-tools/mage TestGoTest_Helper_AssertOutput/assert_fails.*
196-
gotest_test.go:\d+:.*
197-
Error Trace: gotest_test.go:\d+.*
196+
.*gotest_test.go:\d+:.*
197+
Error Trace: .*gotest_test.go:\d+.*
198198
Error: Should be true.*
199199
Test: TestGoTest_Helper_AssertOutput/assert_fails.*
200200
--- FAIL: TestGoTest_Helper_AssertOutput/assert_fails .*
201201
=== FAIL: dev-tools/mage TestGoTest_Helper_AssertOutput/assert_with_message .*
202-
gotest_test.go:\d+:.*
203-
Error Trace: gotest_test.go:\d+.*
202+
.*gotest_test.go:\d+:.*
203+
Error Trace: .*gotest_test.go:\d+.*
204204
Error: Should be true.*
205205
Test: TestGoTest_Helper_AssertOutput/assert_with_message.*
206206
Messages: My message.*
207207
--- FAIL: TestGoTest_Helper_AssertOutput/assert_with_message .*
208208
=== FAIL: dev-tools/mage TestGoTest_Helper_AssertOutput/assert_with_messagef .*
209-
gotest_test.go:\d+:.*
210-
Error Trace: gotest_test.go:\d+.*
209+
.*gotest_test.go:\d+:.*
210+
Error Trace: .*gotest_test.go:\d+.*
211211
Error: Should be true.*
212212
Test: TestGoTest_Helper_AssertOutput/assert_with_messagef.*
213213
Messages: My message with arguments: 42.*
214214
--- FAIL: TestGoTest_Helper_AssertOutput/assert_with_messagef .*
215215
=== FAIL: dev-tools/mage TestGoTest_Helper_AssertOutput/require_fails .*
216-
gotest_test.go:\d+:.*
217-
Error Trace: gotest_test.go:\d+.*
216+
.*gotest_test.go:\d+:.*
217+
Error Trace: .*gotest_test.go:\d+.*
218218
Error: Should be true.*
219219
Test: TestGoTest_Helper_AssertOutput/require_fails.*
220220
--- FAIL: TestGoTest_Helper_AssertOutput/require_fails .*
221221
=== FAIL: dev-tools/mage TestGoTest_Helper_AssertOutput/require_with_message .*
222-
gotest_test.go:\d+:.*
223-
Error Trace: gotest_test.go:\d+.*
222+
.*gotest_test.go:\d+:.*
223+
Error Trace: .*gotest_test.go:\d+.*
224224
Error: Should be true.*
225225
Test: TestGoTest_Helper_AssertOutput/require_with_message.*
226226
Messages: My message.*
227227
--- FAIL: TestGoTest_Helper_AssertOutput/require_with_message .*
228228
=== FAIL: dev-tools/mage TestGoTest_Helper_AssertOutput/require_with_messagef .*
229-
gotest_test.go:\d+:.*
230-
Error Trace: gotest_test.go:\d+.*
229+
.*gotest_test.go:\d+:.*
230+
Error Trace: .*gotest_test.go:\d+.*
231231
Error: Should be true.*
232232
Test: TestGoTest_Helper_AssertOutput/require_with_messagef.*
233233
Messages: My message with arguments: 42.*
234234
--- FAIL: TestGoTest_Helper_AssertOutput/require_with_messagef .*
235235
=== FAIL: dev-tools/mage TestGoTest_Helper_AssertOutput/equals_map .*
236-
gotest_test.go:\d+:.*
237-
Error Trace: gotest_test.go:\d+.*
236+
.*gotest_test.go:\d+:.*
237+
Error Trace: .*gotest_test.go:\d+.*
238238
Error: Not equal:.*
239239
\s+expected: map\[string\]interface \{\}\{"a":1, "b":true, "c":"test", "e":map\[string\]interface \{\}\{"x":"y"\}\}.*
240240
\s+actual : map\[string\]interface \{\}\{"a":42, "b":false, "c":"test"\}.*

go.mod

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ require (
1414
github.com/elastic/e2e-testing v1.99.2-0.20221205111528-ade3c840d0c0
1515
github.com/elastic/elastic-agent-autodiscover v0.2.1
1616
github.com/elastic/elastic-agent-client/v7 v7.0.2-0.20221129150247-15881a8e64ef
17-
github.com/elastic/elastic-agent-libs v0.2.16
17+
github.com/elastic/elastic-agent-libs v0.3.6
1818
github.com/elastic/elastic-agent-system-metrics v0.4.4
1919
github.com/elastic/go-licenser v0.4.0
2020
github.com/elastic/go-sysinfo v1.8.1
@@ -30,7 +30,7 @@ require (
3030
github.com/kardianos/service v1.2.1-0.20210728001519-a323c3813bc7
3131
github.com/magefile/mage v1.14.0
3232
github.com/mitchellh/gox v1.0.1
33-
github.com/mitchellh/hashstructure v0.0.0-20170116052023-ab25296c0f51
33+
github.com/mitchellh/hashstructure v1.1.0
3434
github.com/mitchellh/mapstructure v1.5.0
3535
github.com/oklog/ulid v1.3.1
3636
github.com/otiai10/copy v1.2.0
@@ -40,7 +40,7 @@ require (
4040
github.com/shirou/gopsutil/v3 v3.21.12
4141
github.com/sirupsen/logrus v1.8.1
4242
github.com/spf13/cobra v1.3.0
43-
github.com/stretchr/testify v1.7.0
43+
github.com/stretchr/testify v1.8.2
4444
github.com/tsg/go-daemon v0.0.0-20200207173439-e704b93fd89b
4545
go.elastic.co/apm/module/apmgorilla v1.15.0
4646
go.elastic.co/ecszap v1.0.1
@@ -115,7 +115,7 @@ require (
115115
github.com/santhosh-tekuri/jsonschema v1.2.4 // indirect
116116
github.com/sergi/go-diff v1.1.0 // indirect
117117
github.com/spf13/pflag v1.0.5 // indirect
118-
github.com/stretchr/objx v0.2.0 // indirect
118+
github.com/stretchr/objx v0.5.0 // indirect
119119
github.com/tklauser/go-sysconf v0.3.9 // indirect
120120
github.com/tklauser/numcpus v0.3.0 // indirect
121121
github.com/yusufpapurcu/wmi v1.2.2 // indirect

go.sum

+11-6
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,8 @@ github.com/elastic/elastic-agent-autodiscover v0.2.1/go.mod h1:gPnzzfdYNdgznAb+i
400400
github.com/elastic/elastic-agent-client/v7 v7.0.2-0.20221129150247-15881a8e64ef h1:+3AWaimDL826eoU06qOFBtA3xmyuTr9YUMVWvnim4mU=
401401
github.com/elastic/elastic-agent-client/v7 v7.0.2-0.20221129150247-15881a8e64ef/go.mod h1:cHviLpA5fAwMbfBIHBVNl16qp90bO7pKHMAQaG+9raU=
402402
github.com/elastic/elastic-agent-libs v0.2.5/go.mod h1:chO3rtcLyGlKi9S0iGVZhYCzDfdDsAQYBc+ui588AFE=
403-
github.com/elastic/elastic-agent-libs v0.2.16 h1:31/VlnRp2ko0CMg+sm7b/u3vOPIlFXgMkJn/8uS6EbU=
404-
github.com/elastic/elastic-agent-libs v0.2.16/go.mod h1:0J9lzJh+BjttIiVjYDLncKYCEWUUHiiqnuI64y6C6ss=
403+
github.com/elastic/elastic-agent-libs v0.3.6 h1:PxV0+dq92vzM96qNQCfwsMMqWcdaGc9nbuTquv409Ws=
404+
github.com/elastic/elastic-agent-libs v0.3.6/go.mod h1:h48hzjQcn6XPwfWRM5HimAKlsG0J92ULgAzdX+WedA8=
405405
github.com/elastic/elastic-agent-system-metrics v0.4.4 h1:Br3S+TlBhijrLysOvbHscFhgQ00X/trDT5VEnOau0E0=
406406
github.com/elastic/elastic-agent-system-metrics v0.4.4/go.mod h1:tF/f9Off38nfzTZHIVQ++FkXrDm9keFhFpJ+3pQ00iI=
407407
github.com/elastic/elastic-package v0.36.0/go.mod h1:TUyhRXtf+kazrUthMF+5FtngcFJZtsgty0o/nnl8UFU=
@@ -909,8 +909,8 @@ github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUb
909909
github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg=
910910
github.com/mitchellh/gox v1.0.1 h1:x0jD3dcHk9a9xPSDN6YEL4xL6Qz0dvNYm8yZqui5chI=
911911
github.com/mitchellh/gox v1.0.1/go.mod h1:ED6BioOGXMswlXa2zxfh/xdd5QhwYliBFn9V18Ap4z4=
912-
github.com/mitchellh/hashstructure v0.0.0-20170116052023-ab25296c0f51 h1:qdHlMllk/PTLUrX3XdtXDrLL1lPSfcqUmJD1eYfbapg=
913-
github.com/mitchellh/hashstructure v0.0.0-20170116052023-ab25296c0f51/go.mod h1:QjSHrPWS+BGUVBYkbTZWEnOh3G1DutKwClXU/ABz6AQ=
912+
github.com/mitchellh/hashstructure v1.1.0 h1:P6P1hdjqAAknpY/M1CGipelZgp+4y9ja9kmUZPXP+H0=
913+
github.com/mitchellh/hashstructure v1.1.0/go.mod h1:xUDAozZz0Wmdiufv0uyhnHkUTN6/6d8ulp4AwfLKrmA=
914914
github.com/mitchellh/iochan v1.0.0 h1:C+X3KsSTLFVBr/tK1eYN/vs4rJcvsiLU338UhYPJWeY=
915915
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
916916
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
@@ -1162,8 +1162,10 @@ github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag
11621162
github.com/stretchr/objx v0.0.0-20180129172003-8a3f7159479f/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
11631163
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
11641164
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1165-
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
11661165
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
1166+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
1167+
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
1168+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
11671169
github.com/stretchr/testify v0.0.0-20180303142811-b89eecf5ca5d/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
11681170
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
11691171
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
@@ -1172,8 +1174,11 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
11721174
github.com/stretchr/testify v1.5.0/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
11731175
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
11741176
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
1175-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
11761177
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
1178+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
1179+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
1180+
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
1181+
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
11771182
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
11781183
github.com/syndtr/gocapability v0.0.0-20170704070218-db04d3cc01c8/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
11791184
github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=

internal/pkg/agent/cmd/common.go

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func NewCommandWithArgs(args []string, streams *cli.IOStreams) *cobra.Command {
7878
cmd.AddCommand(reexec)
7979
}
8080
cmd.Run = run.Run
81+
cmd.RunE = run.RunE
8182

8283
return cmd
8384
}

internal/pkg/agent/cmd/run.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func newRunCommandWithArgs(_ []string, streams *cli.IOStreams) *cobra.Command {
6161
cmd := &cobra.Command{
6262
Use: "run",
6363
Short: "Start the elastic-agent.",
64-
Run: func(cmd *cobra.Command, _ []string) {
64+
RunE: func(cmd *cobra.Command, _ []string) error {
6565
// done very early so the encrypted store is never used
6666
disableEncryptedStore, _ := cmd.Flags().GetBool("disable-encrypted-store")
6767
if disableEncryptedStore {
@@ -72,11 +72,9 @@ func newRunCommandWithArgs(_ []string, streams *cli.IOStreams) *cobra.Command {
7272
if err := run(nil, testingMode); err != nil && !errors.Is(err, context.Canceled) {
7373
fmt.Fprintf(streams.Err, "Error: %v\n%s\n", err, troubleshootMessage())
7474

75-
// TODO: remove it. os.Exit will be called on main and if it's called
76-
// too early some goroutines with deferred functions related
77-
// to the shutdown process might not run.
78-
os.Exit(1)
75+
return err
7976
}
77+
return nil
8078
},
8179
}
8280

@@ -121,7 +119,9 @@ func run(override cfgOverrider, testingMode bool, modifiers ...component.Platfor
121119
var stopBeat = func() {
122120
close(stop)
123121
}
124-
service.HandleSignals(stopBeat, cancel)
122+
123+
defer cancel()
124+
go service.ProcessWindowsControlEvents(stopBeat)
125125

126126
cfg, err := loadConfig(override)
127127
if err != nil {
@@ -277,7 +277,7 @@ LOOP:
277277
for {
278278
select {
279279
case <-stop:
280-
l.Info("service.HandleSignals invoked stop function. Shutting down")
280+
l.Info("service.ProcessWindowsControlEvents invoked stop function. Shutting down")
281281
break LOOP
282282
case <-appDone:
283283
l.Info("application done, coordinator exited")
@@ -417,6 +417,7 @@ func defaultLogLevel(cfg *configuration.Configuration) string {
417417
func tryDelayEnroll(ctx context.Context, logger *logger.Logger, cfg *configuration.Configuration, override cfgOverrider) (*configuration.Configuration, error) {
418418
enrollPath := paths.AgentEnrollFile()
419419
if _, err := os.Stat(enrollPath); err != nil {
420+
//nolint:nilerr // ignore the error, this is expected
420421
// no enrollment file exists or failed to stat it; nothing to do
421422
return cfg, nil
422423
}

main.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,31 @@ import (
1616

1717
// Setups and Runs agent.
1818
func main() {
19-
if err := cmd.CheckNativePlatformCompat(); err != nil {
19+
var err error
20+
defer func() {
21+
if err != nil {
22+
os.Exit(1) // defer os exit and allow other goroutines to cleanup
23+
}
24+
}()
25+
26+
err = cmd.CheckNativePlatformCompat()
27+
if err != nil {
2028
fmt.Fprintf(os.Stderr, "Failed to initialize: %v\n", err)
21-
os.Exit(1)
29+
return
2230
}
2331

2432
pj, err := process.CreateJobObject()
2533
if err != nil {
2634
fmt.Fprintf(os.Stderr, "Failed to initialize process job object: %v\n", err)
27-
os.Exit(1)
35+
return
2836
}
2937
defer pj.Close()
3038

3139
rand.Seed(time.Now().UnixNano())
3240
command := cmd.NewCommand()
33-
if err := command.Execute(); err != nil {
41+
err = command.Execute()
42+
if err != nil {
3443
fmt.Fprintf(os.Stderr, "%v\n", err)
35-
os.Exit(1)
44+
return
3645
}
3746
}

0 commit comments

Comments
 (0)