In lib.Handler, close resources even in case of an error.

This is issue UCB-02-001 from the 2021 security audit of Turbo Tunnel by
Cure53.

Closes #40042.
4 jobs for 40042-lib-handler-fails-to-release-resources-if-opening-stream-fails in 6 minutes and 7 seconds (queued for 2 seconds)
Status Job ID Name Coverage
  Test
passed #18649
android

00:06:07

passed #18652
debian-testing

00:03:06

passed #18650
go-1.13

00:01:38

passed #18651
go-1.14

00:01:38