Skip to content

Commit

Permalink
feat(ffi) produce an error on 'shm:set()' when already locked
Browse files Browse the repository at this point in the history
Catch the case and produce an error instead of entering a deadlock.
  • Loading branch information
casimiro authored and thibaultcha committed Oct 30, 2024
1 parent 630a5e7 commit 741b22a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
4 changes: 4 additions & 0 deletions lib/resty/wasmx/shm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ local function shm_kv_set(zone, key, value, cas)
return nil, "no memory"
end

if rc == FFI_ABORT then
return nil, "locked"
end

assert_debug(rc == FFI_OK)

return tonumber(written[0])
Expand Down
5 changes: 5 additions & 0 deletions src/common/lua/ngx_wasm_lua_ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,11 @@ ngx_wa_ffi_shm_kv_set(ngx_wa_shm_t *shm, ngx_str_t *k, ngx_str_t *v,

ngx_wa_assert(shm->type == NGX_WA_SHM_TYPE_KV);

if (ngx_wa_shm_locked(shm)) {
/* already locked by the current worker */
return NGX_ABORT;
}

ngx_wa_shm_lock(shm);

rc = ngx_wa_shm_kv_set_locked(shm, k, v, cas, written);
Expand Down
12 changes: 6 additions & 6 deletions t/04-openresty/ffi/shm/002-kv_set.t
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ cas must be a number


=== TEST 4: shm_kv - set() on locked shm
--- timeout: 1
--- abort
--- shm_kv: kv 16k
--- config
location /t {
Expand All @@ -115,15 +113,17 @@ cas must be a number

shm.kv:lock()

shm.kv:set("kv/k1", "v1")
local written, err = shm.kv:set("kv/k1", "v1")

shm.kv:unlock()

ngx.say("fail")
assert(written == nil)

ngx.say(err)
}
}
--- error_code:
--- response_body
locked
--- no_error_log
[error]
[crit]
[emerg]

0 comments on commit 741b22a

Please sign in to comment.