Cyclic graphs will never finish the topological sort so we should check the cycle before doing the topological sort. Imported from Jira [BEAM-4834](https://issues.apache.org/jira/browse/BEAM-4834). Original Jira may contain additional context. Reported by: angoenka.