Make linenoise safe

Wrap calls into linenoise in a mutex so that the functions don't have to be `unsafe` any more (fixes #3921), and stop leaking every line that linenoise reads.