Skip to content

Commit f75eead

Browse files
fix: crashes when wrapper encounters an error.
1 parent 95be348 commit f75eead

3 files changed

Lines changed: 29 additions & 8 deletions

File tree

decrypt.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"github.com/sirupsen/logrus"
56
"sync"
67
)
@@ -56,6 +57,14 @@ func (d *Dispatcher) RemoveInstance(id string) {
5657

5758
func (d *Dispatcher) Submit(task *Task) {
5859
inst := d.selectInstance(task.AdamId)
60+
if inst == nil {
61+
task.Result <- &Result{
62+
Success: false,
63+
Data: task.Payload,
64+
Error: fmt.Errorf("no available instance"),
65+
}
66+
return
67+
}
5968
inst.Process(task)
6069
}
6170

decrypt_instance.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type DecryptInstance struct {
2525
LastAdamId string
2626
LastKey string
2727
LastHandleTime time.Time
28+
closeOnce sync.Once
2829
Available bool
2930
}
3031

@@ -45,14 +46,16 @@ func NewDecryptInstance(inst *WrapperInstance) (*DecryptInstance, error) {
4546
}
4647

4748
func (d *DecryptInstance) Unavailable() {
48-
err := d.conn.Close()
49-
if err != nil {
50-
logrus.Errorf("failed to close conn of insttance %s: %s", d.id, err)
51-
}
52-
err = KillWrapper(d.id)
53-
if err != nil {
54-
logrus.Errorf("failed to kill insttance %s: %s", d.id, err)
55-
}
49+
d.closeOnce.Do(func() {
50+
err := d.conn.Close()
51+
if err != nil {
52+
logrus.Errorf("failed to close conn of insttance %s: %s", d.id, err)
53+
}
54+
err = KillWrapper(d.id)
55+
if err != nil {
56+
logrus.Errorf("failed to kill insttance %s: %s", d.id, err)
57+
}
58+
})
5659
}
5760

5861
func (d *DecryptInstance) GetLastAdamId() string {

wrapper.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,15 @@ func wrapperDown(instance *WrapperInstance) {
204204

205205
func KillWrapper(id string) error {
206206
instance := GetInstance(id)
207+
if instance == nil {
208+
return fmt.Errorf("instance %s not found", id)
209+
}
210+
if instance.Cmd == nil {
211+
return fmt.Errorf("instance %s cmd is nil", id)
212+
}
213+
if instance.Cmd.Process == nil {
214+
return fmt.Errorf("instance %s process is nil", id)
215+
}
207216
return instance.Cmd.Process.Kill()
208217
}
209218

0 commit comments

Comments
 (0)