commons: make use try-with-resource in couters

Acked-by: Paul Millar
Target: master
Require-book: no
Require-notes: no