bungle/lua-resty-session

problems with using shm storage

d80x86 opened this issue · 2 comments

centos 7
openresty - 1.19.3.2
lua-resty-session - 3.8

nginx.conf

lua_shared_dict sessions    10m;

test.lua

local opts = {
   storage = 'shm',
   shm = { store = 'sessions' }
 }

 local ses = require 'resty.session'.start(opts)
 ses.data.v = 'ccav'
 ses:save()

 ngx.print('ok')

'OK' is correct only the first time
The second time: throw error attempt to index local 'ses' (a nil value)
The third time: hang up until timeout(5 second), and print 'OK'

subsequent result: throw error -> hang up -> throw error -> hang up .....

I meet the same issue: ### The second time: throw error attempt to index local 'ses' (a nil value)
I think the root cause is that:

  1. session.start(opts) -> session.open(opts, true) -> self.strategy.open(self, cookie, true) will lock the shm,
  2. then self.strategy.start(self) want to lock the shm again, and it return an error "locked", so that session.start return nil, "locked"

https://github.com/bungle/lua-resty-session/blob/master/lib/resty/session.lua#L615

Work with code as following:
https://github.com/bungle/lua-resty-session#session-present-reason--sessionopenopts-keep_lock

local ses = require 'resty.session'.open(opts)
-- Now let's really start the session
if not ses.started then 
    ses:start()
end

ses.data.v = 'ccav'
ses:save()

ngx.print('ok')

Yes, using session.open() is fine
However, calling session.start() directly is problematic, and will be locked until timeout.
Normally, after session.save() is called, locked should be turned off.