Fixed potential race condition if encoding on concurrent threads