Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ATO-554 add BlockingIOError handler, tests and portalocker #12271

Conversation

Urkem
Copy link
Contributor

@Urkem Urkem commented Apr 12, 2023

Proposed changes:

  • Fix ATO-554 by obtaining the STDOUT lock with portalocker, as it blocks only half way on utterances with more than ~5KB of text.

Status (please check what you already did):

  • added some tests for the functionality
  • updated the documentation
  • updated the changelog (please check changelog for instructions)
  • reformat files using black (please check Readme for instructions)

@Urkem Urkem requested a review from a team as a code owner April 12, 2023 10:59
Copy link
Member

@ancalita ancalita left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍🏻
Left some suggestions in the comments.

@@ -0,0 +1 @@
Fix `BlockingIOError` when running `rasa cli` on utterances with more than 5KB of text.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be more specific about which CLI command causes this issue (I think it's rasa shell, could also be rasa interactive), please double-check with @trammell.

pyproject.toml Show resolved Hide resolved
rasa/shared/utils/cli.py Show resolved Hide resolved

mock = Mock()
monkeypatch.setattr(builtins, "print", mock_print)
monkeypatch.setattr(rasa.shared.utils.io, "handle_print_blocking", mock)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're mocking the function we want to test, we're not actually testing it, but I'd suggest to at least assert that mock was called with the desired output (in addition to the call_count assertion).
Also pytest has a good fixture for capturing stdout: capsys (https://docs.pytest.org/en/7.1.x/how-to/capture-stdout-stderr.html), have you tried with that too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried with capsys but it captures only the text output to stdout/stderr and not the system calls to lock/unlock stdout. After trying with capsys and capfd I decided to mock it but there was no good way (that I know of) to assert that lock/unlock happened so I was left with this...


# Can't manipulate the STDOUT file descriptor
# in pytest (it's a io.StringIO instance not a file)
def test_handle_print_blocking(monkeypatch: MonkeyPatch):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a test of handle_print_blocking but rather of the function which is calling it. Please move this test to the proper module and rename it to match the test case of parent caller.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the tests to -> test_cli.py and renamed appropriately

@@ -626,3 +625,21 @@ def is_subdirectory(path: Text, potential_parent_directory: Text) -> bool:
def random_string(length: int) -> Text:
"""Returns a random string of given length."""
return "".join(random.choices(string.ascii_uppercase + string.digits, k=length))


def handle_print_blocking(output: Text) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We must add unit test for this function to make sure that:

  • portalocker.Lock is called properly - mock Lock function
  • AnsiToWin32 is called properly if sys.platform == "win32"
  • print is called with proper output, file and flush parameters

@radovanZRasa
Copy link
Contributor

One Note:
If we were able to reproduce the issue, why couldn't we write the test which reproduces it?

@Urkem
Copy link
Contributor Author

Urkem commented Apr 13, 2023

@radovanZRasa I was not able to reproduce it 100% of the time It happens mostly on MacOS python 3.8 80% of the time and on 3.7 3.9 3.10 I was able to reproduce it 5-10% of the time. On Windows I could not reproduce it (I raised the exception explicitly)... @trammell stated in the jira ticket he is not able to reproduce it at will

@Urkem Urkem requested a review from radovanZRasa April 18, 2023 07:25
@github-actions
Copy link
Contributor

🚀 A preview of the docs have been deployed at the following URL: https://12271--rasahq-docs-rasa-v2.netlify.app/docs/rasa

@Urkem Urkem merged commit 8a48f2b into 3.5.x Apr 19, 2023
113 checks passed
@Urkem Urkem deleted the ATO-554-blocking-io-error-in-rasa-cli-with-large-1-k-utterance-length branch April 19, 2023 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants