It looks like politely calls options to set the user agent prior to running the wrapped function and then calls options again afterward to restore the previous value. However, if the wrapped function throws an error, the second call to options never happens and the user agent is not restored. I believe the correct way to handle this is with on.exit, e.g.:
old_ua <- getOption("HTTPUserAgent")
on.exit(options("HTTPUserAgent"= old_ua))
options("HTTPUserAgent"= user_agent)
res <- mem_fun(...)