Fix fd leak, add unit test for channel passing