Avoid occasionnal deadlocks (1 in 256)

This commit is contained in:
Alex 2020-03-13 11:29:03 +01:00
parent 6292303a31
commit ad8461491b
3 changed files with 9 additions and 9 deletions

View File

@ -453,17 +453,16 @@ func (a *Account) eventInternal(event *Event) error {
var cache_key string var cache_key string
if event.Id != "" { if event.Id != "" {
// Use mx_room_id as a lock slot key, because this section is
// concurrent with the part that sends events out in server.go,
// and at that point we don't know the event's ID yet
// since it will be attributed by the backend during the call to send
dbLockSlot(mx_room_id) dbLockSlot(mx_room_id)
defer dbUnlockSlot(mx_room_id) defer dbUnlockSlot(mx_room_id)
// If the event has an ID, make sure it is processed only once // If the event has an ID, make sure it is processed only once
cache_key = fmt.Sprintf("%s/event_seen/%s/%s", cache_key = fmt.Sprintf("%s/event_seen/%s/%s",
a.Protocol, mx_room_id, event.Id) a.Protocol, mx_room_id, event.Id)
slot_key := dbKvSlotKey(cache_key)
dbLockSlot(slot_key)
defer dbUnlockSlot(slot_key)
if dbKvGet(cache_key) == "yes" { if dbKvGet(cache_key) == "yes" {
// false: cache key was not modified, meaning we // false: cache key was not modified, meaning we
// already saw the event // already saw the event

View File

@ -166,6 +166,7 @@ func handleTxnEvent(e *mxlib.Event) error {
ev.Author = acct.Conn.User() ev.Author = acct.Conn.User()
ev.Room = room.RoomID ev.Room = room.RoomID
// use room id as lock slot key, see account.go in eventInternal
dbLockSlot(e.RoomId) dbLockSlot(e.RoomId)
defer dbUnlockSlot(e.RoomId) defer dbUnlockSlot(e.RoomId)
@ -173,7 +174,7 @@ func handleTxnEvent(e *mxlib.Event) error {
if err == nil && created_ev_id != "" { if err == nil && created_ev_id != "" {
cache_key := fmt.Sprintf("%s/event_seen/%s/%s", cache_key := fmt.Sprintf("%s/event_seen/%s/%s",
room.Protocol, e.RoomId, created_ev_id) room.Protocol, e.RoomId, created_ev_id)
dbKvPut(cache_key, "yes") dbKvPutLocked(cache_key, "yes")
} }
return err return err
} else { } else {

View File

@ -27,15 +27,15 @@ func ezbrSystemRoom(user_mx_id string) (string, error) {
} }
widget_kv_key := "ezbr_widget_on:" + mx_room_id widget_kv_key := "ezbr_widget_on:" + mx_room_id
if config.WebURL != "" && dbKvTestAndSet(widget_kv_key, "yes") { if config.WebURL != "" && dbKvGet(widget_kv_key) != "yes" {
widget := map[string]interface{}{ widget := map[string]interface{}{
"type": "m.easybridge", "type": "m.easybridge",
"url": config.WebURL, "url": config.WebURL,
"name": "Easybridge account configuration dashboard", "name": "Easybridge account configuration dashboard",
} }
err = mx.PutStateAs(mx_room_id, "im.vector.modular.widgets", "ezbr_widget", widget, ezbrMxId()) err = mx.PutStateAs(mx_room_id, "im.vector.modular.widgets", "ezbr_widget", widget, ezbrMxId())
if err != nil { if err == nil {
dbKvPut(widget_kv_key, "") dbKvPut(widget_kv_key, "yes")
} }
} }