@agoric/cosmos 0.34.2-dev-1578064.0 → 0.34.2-dev-8c6c8c2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,6 +15,7 @@ import (
15
15
  "net/rpc"
16
16
  "os"
17
17
  "path/filepath"
18
+ "runtime/debug"
18
19
 
19
20
  log "github.com/cometbft/cometbft/libs/log"
20
21
 
@@ -25,6 +26,11 @@ import (
25
26
  servertypes "github.com/cosmos/cosmos-sdk/server/types"
26
27
  )
27
28
 
29
+ // Taken from sysexits.h
30
+ const (
31
+ EX_SOFTWARE = 70 /* internal software error */
32
+ )
33
+
28
34
  type goReturn = struct {
29
35
  str string
30
36
  err error
@@ -32,6 +38,8 @@ type goReturn = struct {
32
38
 
33
39
  const SwingSetPort = 123
34
40
 
41
+ var logger = log.NewTMLogger(log.NewSyncWriter(os.Stderr)).With("module", "cmd/libdaemon")
42
+
35
43
  var vmClientCodec *vm.ClientCodec
36
44
  var agdServer *vm.AgdServer
37
45
 
@@ -59,8 +67,29 @@ func ConnectVMClientCodec(ctx context.Context, nodePort int, sendFunc func(int,
59
67
  return vmClientCodec, sendToNode
60
68
  }
61
69
 
70
+ // handlePanic is a helper function to recover from panics, log them, and exit the process.
71
+ func handlePanic(caller string) {
72
+ if r := recover(); r != nil {
73
+ defer func() {
74
+ if err := recover(); err != nil {
75
+ // If we panic again, we will exit the process without logging the error or stack.
76
+ os.Stderr.WriteString("Double panic in exported Go function: " + caller + "\n")
77
+ os.Exit(EX_SOFTWARE)
78
+ }
79
+ }()
80
+
81
+ // Log the panic with the caller information.
82
+ logger.Error("Panic in exported Go function", "caller", caller, "error", r, "stack", debug.Stack())
83
+
84
+ // Exit the process with a non-zero exit code.
85
+ os.Exit(EX_SOFTWARE)
86
+ }
87
+ }
88
+
62
89
  //export RunAgCosmosDaemon
63
90
  func RunAgCosmosDaemon(nodePort C.int, toNode C.sendFunc, cosmosArgs []*C.char) C.int {
91
+ defer handlePanic("RunAgCosmosDaemon")
92
+
64
93
  userHomeDir, err := os.UserHomeDir()
65
94
  if err != nil {
66
95
  panic(err)
@@ -92,6 +121,8 @@ func RunAgCosmosDaemon(nodePort C.int, toNode C.sendFunc, cosmosArgs []*C.char)
92
121
  // fmt.Fprintln(os.Stderr, "Starting Cosmos", args)
93
122
  os.Args = args
94
123
  go func() {
124
+ defer handlePanic("daemon.RunWithController")
125
+
95
126
  // We run in the background, but exit when the job is over.
96
127
  // swingset.SendToNode("hello from Initial Go!")
97
128
  exitCode := 0
@@ -112,6 +143,7 @@ func RunAgCosmosDaemon(nodePort C.int, toNode C.sendFunc, cosmosArgs []*C.char)
112
143
 
113
144
  //export ReplyToGo
114
145
  func ReplyToGo(replyPort C.int, isError C.int, resp C.Body) C.int {
146
+ defer handlePanic("ReplyToGo")
115
147
  respStr := C.GoString(resp)
116
148
  // fmt.Printf("Reply to Go %d %s\n", replyPort, respStr)
117
149
  if err := vmClientCodec.Receive(int(replyPort), int(isError) != 0, respStr); err != nil {
@@ -126,6 +158,7 @@ type errorWrapper struct {
126
158
 
127
159
  //export SendToGo
128
160
  func SendToGo(port C.int, msg C.Body) C.Body {
161
+ defer handlePanic("SendToGo")
129
162
  msgStr := C.GoString(msg)
130
163
  // fmt.Fprintln(os.Stderr, "Send to Go", msgStr)
131
164
  var respStr string
package/git-revision.txt CHANGED
@@ -1 +1 @@
1
- 1578064
1
+ 8c6c8c2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agoric/cosmos",
3
- "version": "0.34.2-dev-1578064.0+1578064",
3
+ "version": "0.34.2-dev-8c6c8c2.0+8c6c8c2",
4
4
  "description": "Connect JS to the Cosmos blockchain SDK",
5
5
  "parsers": {
6
6
  "js": "mjs"
@@ -41,5 +41,5 @@
41
41
  "typeCoverage": {
42
42
  "atLeast": 0
43
43
  },
44
- "gitHead": "1578064491a1f19cbd45f8ec5b85476077ffaa01"
44
+ "gitHead": "8c6c8c2b5b2e83634c328fdb9abc4de50c8d6165"
45
45
  }
package/vm/client.go CHANGED
@@ -4,6 +4,7 @@ import (
4
4
  "context"
5
5
  "fmt"
6
6
  "net/rpc"
7
+ "sync"
7
8
  )
8
9
 
9
10
  // ReceiveMessageMethod is the name of the method we call in order to have the
@@ -34,12 +35,21 @@ var _ rpc.ClientCodec = (*ClientCodec)(nil)
34
35
  // having the WriteRequest() method fabricate a Receive() call to clear the rpc
35
36
  // state.
36
37
  type ClientCodec struct {
37
- ctx context.Context
38
- send func(port, rPort int, msg string)
39
- outbound map[int]rpc.Request
40
- inbound chan *rpc.Response
41
- replies map[uint64]string
38
+ ctx context.Context
39
+ send func(port, rPort int, msg string)
40
+ inbound chan *rpc.Response
41
+
42
+ // reqMutex protects outbound requests.
43
+ reqMutex sync.Mutex
44
+ outbound map[int]rpc.Request
45
+
46
+ // Scratch space to communicate between ReadResponseHeader and
47
+ // ReadResponseBody (protected by mutex in "net/rpc" implementation).
42
48
  replyToRead uint64
49
+
50
+ // mutex protects replies map
51
+ mutex sync.Mutex
52
+ replies map[uint64]string
43
53
  }
44
54
 
45
55
  // NewClientCodec creates a new ClientCodec.
@@ -64,7 +74,11 @@ func (cc *ClientCodec) WriteRequest(r *rpc.Request, body interface{}) error {
64
74
  return fmt.Errorf("body %T is not a Message", body)
65
75
  }
66
76
  rPort := int(r.Seq + 1) // rPort is 1-indexed to indicate it's required
77
+
78
+ cc.reqMutex.Lock()
67
79
  cc.outbound[rPort] = *r
80
+ cc.reqMutex.Unlock()
81
+
68
82
  var senderReplyPort int
69
83
  if msg.NeedsReply {
70
84
  senderReplyPort = rPort
@@ -85,18 +99,27 @@ func (cc *ClientCodec) ReadResponseHeader(r *rpc.Response) error {
85
99
  }
86
100
 
87
101
  // ReadResponseBody decodes a response body (currently just string) from the VM.
102
+ // and will always be called immediately after ReadResponseHeader (cf.
103
+ // https://pkg.go.dev/net/rpc#ClientCodec ).
88
104
  func (cc *ClientCodec) ReadResponseBody(body interface{}) error {
105
+ cc.mutex.Lock()
106
+ reply := cc.replies[cc.replyToRead]
107
+ delete(cc.replies, cc.replyToRead)
108
+ cc.mutex.Unlock()
109
+
89
110
  if body != nil {
90
- *body.(*string) = cc.replies[cc.replyToRead]
111
+ *body.(*string) = reply
91
112
  }
92
- delete(cc.replies, cc.replyToRead)
93
113
  return nil
94
114
  }
95
115
 
96
116
  // Receive is called by the VM to send a response to the client.
97
117
  func (cc *ClientCodec) Receive(rPort int, isError bool, data string) error {
118
+ cc.reqMutex.Lock()
98
119
  outb := cc.outbound[rPort]
99
120
  delete(cc.outbound, rPort)
121
+ cc.reqMutex.Unlock()
122
+
100
123
  resp := &rpc.Response{
101
124
  ServiceMethod: outb.ServiceMethod,
102
125
  Seq: outb.Seq,
@@ -104,7 +127,9 @@ func (cc *ClientCodec) Receive(rPort int, isError bool, data string) error {
104
127
  if isError {
105
128
  resp.Error = data
106
129
  } else {
130
+ cc.mutex.Lock()
107
131
  cc.replies[resp.Seq] = data
132
+ cc.mutex.Unlock()
108
133
  }
109
134
  cc.inbound <- resp
110
135
  return nil