Fix segfault with duplicate handles. Issue #2267

Fix [Issue 2267](https://cfengine.com/dev/issues/2267) and add acceptance test showing failure.