sm: use locks when manipulating the buffer state

also, homogenize the error values