Rework queue handling and error reporting, close #4249

This improves error handling and fixes the Queue issue with Ruby 2.1.x (#4249).