Recursion bug due to module.build() connection tracing

Open Beginner friendly
#709 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
88/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
dart
Domain
tooling

Research direction

Start at Module.build() and the _checkPortConnectionsRecursively entry point, then reproduce the cycle with the test case shown in the issue. Add a regression test for the cyclic hierarchy and verify that build() reports InvalidHierarchyException for the bad parent-to-child connection instead of StackOverflow.

Written by the indexing model from the issue text.

Description

bug
Describe the bug

With some module miswirings we can get a StackOverflow due to infinite recursion.

To Reproduce

Run the following test:

import 'package:rohd/rohd.dart';
import 'package:test/test.dart';

class EmptyModule extends Module {
  EmptyModule({super.name});
}

class RecursivePortCheckRepro extends Module {
  RecursivePortCheckRepro() : super(name: 'top') {
    final driver = Logic();
    final topA = addInput('a', driver);

    final child = EmptyModule(name: 'child');
    final childA = child.addInput('a', topA);

    // The `and()` avoids Logic's immediate direct self-connection rejection
    // while closing the signal graph.
    driver <= childA.and();
  }
}

void main() {
  test('discovers a cyclic hierarchy', () async {
    await expectLater(
      RecursivePortCheckRepro().build(),
      throwsA(isA<InvalidHierarchyException>()),
    );
  });
}
Expected behavior

An error reported on the bad connection in the parent to a child Logic.

Actual behavior

StackOverflow

Additional: Dart SDK info

No response

Additional: pubspec.yaml

Additional: Context

The simplest fix would look like:

void _checkPortConnectionsRecursively({
  Set<Module>? visited,
}) {
  final marked = visited ?? <Module>{};

  // Mark before descending. This breaks cycles such as top -> child -> top.
  if (!marked.add(this)) {
    return;
  }

  _checkPortConnections();

  for (final subModule in _subModules) {
    subModule._checkPortConnectionsRecursively(visited: marked);
  }
}
Dominant language
Dart
Stars
489
Forks
88
Avg merge
3d 23h
Merged PRs (30d)
10

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from intel/rohd

All issues in intel/rohd

Similar issues

More Dart issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.