Skip to content

Commit 789f850

Browse files
committed
Mobile: add mutexMap to protect each namespace
1 parent d58e391 commit 789f850

File tree

1 file changed

+96
-21
lines changed

1 file changed

+96
-21
lines changed

mobile/mobile.go

Lines changed: 96 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ var (
9191

9292
m = make(map[string]*mobileClient)
9393

94-
// mMutex should always be used to guard the m map
95-
mMutex sync.RWMutex
94+
// mMutex should always be used to guard the mutex map
95+
mMutex sync.RWMutex
96+
mutexMap = make(map[string]sync.RWMutex)
9697

9798
registry = make(map[string]func(context.Context,
9899
*grpc.ClientConn, string, func(string, error)))
@@ -173,17 +174,25 @@ func ConnectServer(nameSpace string, mailboxServer string, isDevServer bool,
173174
}
174175
}
175176

176-
mMutex.Lock()
177-
defer mMutex.Unlock()
178-
179-
mc, ok := m[nameSpace]
180-
if !ok {
181-
return fmt.Errorf("unknown namespace: %s", nameSpace)
182-
}
183-
184177
// Since the connection function is blocking, we need to spin it off
185178
// in another goroutine here. See https://pkg.go.dev/syscall/js#FuncOf.
186179
go func() {
180+
mMutex.Lock()
181+
mutex, ok := mutexMap[nameSpace]
182+
mMutex.Unlock()
183+
if !ok {
184+
log.Errorf("Unable to find mutex for namespace: %v", nameSpace)
185+
return
186+
}
187+
mutex.Lock()
188+
defer mutex.Unlock()
189+
190+
mc, ok := m[nameSpace]
191+
if !ok {
192+
log.Errorf("Unknown namespace: %v", nameSpace)
193+
return
194+
}
195+
187196
statusChecker, lndConnect, err := core.MailboxRPCConnection(
188197
mailboxServer, pairingPhrase, localPriv, remotePub,
189198
func(key *btcec.PublicKey) error {
@@ -237,7 +246,13 @@ func ConnectServer(nameSpace string, mailboxServer string, isDevServer bool,
237246
// IsConnected returns whether or not there is an active connection.
238247
func IsConnected(nameSpace string) (bool, error) {
239248
mMutex.Lock()
240-
defer mMutex.Unlock()
249+
mutex, ok := mutexMap[nameSpace]
250+
mMutex.Unlock()
251+
if !ok {
252+
return false, fmt.Errorf("unable to find mutex for namespace: %v", nameSpace)
253+
}
254+
mutex.Lock()
255+
defer mutex.Unlock()
241256

242257
mc, ok := m[nameSpace]
243258
if !ok {
@@ -250,7 +265,13 @@ func IsConnected(nameSpace string) (bool, error) {
250265
// Disconnect closes the RPC connection.
251266
func Disconnect(nameSpace string) error {
252267
mMutex.Lock()
253-
defer mMutex.Unlock()
268+
mutex, ok := mutexMap[nameSpace]
269+
mMutex.Unlock()
270+
if !ok {
271+
return fmt.Errorf("unable to find mutex for namespace: %s", nameSpace)
272+
}
273+
mutex.Lock()
274+
defer mutex.Unlock()
254275

255276
mc, ok := m[nameSpace]
256277
if !ok {
@@ -270,7 +291,13 @@ func Disconnect(nameSpace string) error {
270291
// Status returns the status of the LNC RPC connection.
271292
func Status(nameSpace string) (string, error) {
272293
mMutex.Lock()
273-
defer mMutex.Unlock()
294+
mutex, ok := mutexMap[nameSpace]
295+
mMutex.Unlock()
296+
if !ok {
297+
return "", fmt.Errorf("unable to find mutex for namespace: %v", nameSpace)
298+
}
299+
mutex.Lock()
300+
defer mutex.Unlock()
274301

275302
mc, ok := m[nameSpace]
276303
if !ok {
@@ -290,7 +317,13 @@ func RegisterLocalPrivCreateCallback(nameSpace string,
290317
c NativeCallback) error {
291318

292319
mMutex.Lock()
293-
defer mMutex.Unlock()
320+
mutex, ok := mutexMap[nameSpace]
321+
mMutex.Unlock()
322+
if !ok {
323+
return fmt.Errorf("unable to find mutex for namespace: %s", nameSpace)
324+
}
325+
mutex.Lock()
326+
defer mutex.Unlock()
294327

295328
mc, ok := m[nameSpace]
296329
if !ok {
@@ -308,7 +341,13 @@ func RegisterRemoteKeyReceiveCallback(nameSpace string,
308341
c NativeCallback) error {
309342

310343
mMutex.Lock()
311-
defer mMutex.Unlock()
344+
mutex, ok := mutexMap[nameSpace]
345+
mMutex.Unlock()
346+
if !ok {
347+
return fmt.Errorf("unable to find mutex for namespace: %s", nameSpace)
348+
}
349+
mutex.Lock()
350+
defer mutex.Unlock()
312351

313352
mc, ok := m[nameSpace]
314353
if !ok {
@@ -324,7 +363,13 @@ func RegisterRemoteKeyReceiveCallback(nameSpace string,
324363
// receiving auth data.
325364
func RegisterAuthDataCallback(nameSpace string, c NativeCallback) error {
326365
mMutex.Lock()
327-
defer mMutex.Unlock()
366+
mutex, ok := mutexMap[nameSpace]
367+
mMutex.Unlock()
368+
if !ok {
369+
return fmt.Errorf("unable to find mutex for namespace: %s", nameSpace)
370+
}
371+
mutex.Lock()
372+
defer mutex.Unlock()
328373

329374
mc, ok := m[nameSpace]
330375
if !ok {
@@ -341,7 +386,13 @@ func InvokeRPC(nameSpace string, rpcName string, requestJSON string,
341386
c NativeCallback) error {
342387

343388
mMutex.Lock()
344-
defer mMutex.Unlock()
389+
mutex, ok := mutexMap[nameSpace]
390+
mMutex.Unlock()
391+
if !ok {
392+
return fmt.Errorf("unable to find mutex for namespace: %s", nameSpace)
393+
}
394+
mutex.Lock()
395+
defer mutex.Unlock()
345396

346397
mc, ok := m[nameSpace]
347398
if !ok {
@@ -386,7 +437,13 @@ func InvokeRPC(nameSpace string, rpcName string, requestJSON string,
386437
// GetExpiry returns the expiration time of the connection macaroon.
387438
func GetExpiry(nameSpace string) (string, error) {
388439
mMutex.Lock()
389-
defer mMutex.Unlock()
440+
mutex, ok := mutexMap[nameSpace]
441+
mMutex.Unlock()
442+
if !ok {
443+
return "", fmt.Errorf("unable to find mutex for namespace: %v", nameSpace)
444+
}
445+
mutex.Lock()
446+
defer mutex.Unlock()
390447

391448
mc, ok := m[nameSpace]
392449
if !ok {
@@ -410,7 +467,13 @@ func GetExpiry(nameSpace string) (string, error) {
410467
// IsReadOnly returns whether or not the connection macaroon is read-only.
411468
func IsReadOnly(nameSpace string) (bool, error) {
412469
mMutex.Lock()
413-
defer mMutex.Unlock()
470+
mutex, ok := mutexMap[nameSpace]
471+
mMutex.Unlock()
472+
if !ok {
473+
return false, fmt.Errorf("unable to find mutex for namespace: %v", nameSpace)
474+
}
475+
mutex.Lock()
476+
defer mutex.Unlock()
414477

415478
mc, ok := m[nameSpace]
416479
if !ok {
@@ -438,7 +501,13 @@ func IsReadOnly(nameSpace string) (bool, error) {
438501
// has a specificed permission.
439502
func HasPermissions(nameSpace, permission string) (bool, error) {
440503
mMutex.Lock()
441-
defer mMutex.Unlock()
504+
mutex, ok := mutexMap[nameSpace]
505+
mMutex.Unlock()
506+
if !ok {
507+
return false, fmt.Errorf("unable to find mutex for namespace: %v", nameSpace)
508+
}
509+
mutex.Lock()
510+
defer mutex.Unlock()
442511

443512
mc, ok := m[nameSpace]
444513
if !ok {
@@ -562,7 +631,13 @@ func parseKeys(nameSpace, localPrivKey, remotePubKey string) (
562631
keychain.SingleKeyECDH, *btcec.PublicKey, error) {
563632

564633
mMutex.Lock()
565-
defer mMutex.Unlock()
634+
mutex, ok := mutexMap[nameSpace]
635+
mMutex.Unlock()
636+
if !ok {
637+
return nil, nil, fmt.Errorf("unable to find mutex for namespace: %v", nameSpace)
638+
}
639+
mutex.Lock()
640+
defer mutex.Unlock()
566641

567642
mc, ok := m[nameSpace]
568643
if !ok {

0 commit comments

Comments
 (0)