fix resource cleanup and add testcase of multiple goroutines