diff -r fd1dd79cff20 -r 5537d8f5e989 mercurial/commands.py --- a/mercurial/commands.py Thu Apr 12 23:06:27 2018 -0700 +++ b/mercurial/commands.py Thu Apr 12 23:14:38 2018 -0700 @@ -3089,11 +3089,10 @@ haspatch = False for hunk in patch.split(patchfile): - patchdata = patch.extract(ui, hunk) - - msg, node, rej = cmdutil.tryimportone(ui, repo, patchdata, - parents, opts, - msgs, hg.clean) + with patch.extract(ui, hunk) as patchdata: + msg, node, rej = cmdutil.tryimportone(ui, repo, patchdata, + parents, opts, + msgs, hg.clean) if msg: haspatch = True ui.note(msg + '\n')