It was being called with socket from drain_queue. So the arguments were different in 2 invocations of this function (interesting that dialyzer didn't catch it.
Thought that passing state around is anyway not a good idea, changed it.