Add a timeout decorator for timing out functions that may hang

Fixes #6891