Fixed exception raised if timeout is triggered

A small fix to prevent an exception when a timeout occurs, with test.