Skip to content

Fix the python session to return correct error message when invalid session for MI drivers#2181

Open
zoechanzy wants to merge 6 commits into
ni:masterfrom
zoechanzy:users/zchan/fix-invalid-session-error-description
Open

Fix the python session to return correct error message when invalid session for MI drivers#2181
zoechanzy wants to merge 6 commits into
ni:masterfrom
zoechanzy:users/zchan/fix-invalid-session-error-description

Conversation

@zoechanzy

@zoechanzy zoechanzy commented Jun 29, 2026

Copy link
Copy Markdown
  • This contribution adheres to CONTRIBUTING.md.
  • I've updated CHANGELOG.md if applicable.
  • I've added tests applicable for this pull request

What does this Pull Request accomplish?

  • Fix get_error_description returning "Failed to retrieve error description." when the session is invalid
    • TRY Block 1 — Try get_error (reads session error queue)
      If the session is valid and the error queue has not been overwritten, returned_error_code matches error_code and we return the description immediately.
      • Test: invalid resource name error
      image
    • TRY Block 2 — Try error_message with current session
      Reached if Block 1 fell through — either get_error raised, or it returned a mismatched code (queue was overwritten). Unlike get_error, error_message is a direct lookup by error_code and does not read the error queue. If the session is still valid, this returns the correct description without touching the session handle.
      ( second try statement is exactly same with the second try statement in original file. I only updated the docstring section).
    • TRY Block 3 — Reset session handle to 0, retry error_message
      Reached only when Block 2 also failed — meaning the error_message in TRY block 2 fail to retrieve error due to internal error or it is an invalid session. error_message rejects calls with an invalid session handle, but succeeds with vi=0.
      • No test for real behaviour of valid session in third try block because did not manage to trigger a real internal in valid session that failed in first and second try statement.
  • Updated the mako template and regenerated the affected drivers (nidcpower, nidigital, nidmm, nifake, nifgen, niscope, niswitch).
  • Added nifake unit test to verify the session-handle reset path calls error_message again with vi=0

List issues fixed by this Pull Request below, if any.

Labview error description python error description When session2 is opened after session1, driver will no longer have session handle of session1. It will have session handle of session2. If we use the invalidated session1, the error description between Labview and Python is different. The expected error description of python should be same with Labview, which is "(Hex 0xBFFA1190) The session handle is not valid.".

What testing has been done?

  • Test of invalid session (this bug after fixing):
    image

  • Unit tests and flake8 passed.

@codecov-commenter

codecov-commenter commented Jun 29, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 48 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.65%. Comparing base (ab52ec6) to head (dfb69f2).

Files with missing lines Patch % Lines
...erated/nidcpower/nidcpower/_library_interpreter.py 0.00% 8 Missing ⚠️
...erated/nidigital/nidigital/_library_interpreter.py 0.00% 8 Missing ⚠️
generated/nidmm/nidmm/_library_interpreter.py 0.00% 8 Missing ⚠️
generated/nifgen/nifgen/_library_interpreter.py 0.00% 8 Missing ⚠️
generated/niscope/niscope/_library_interpreter.py 0.00% 8 Missing ⚠️
...enerated/niswitch/niswitch/_library_interpreter.py 0.00% 8 Missing ⚠️

❌ Your patch status has failed because the patch coverage (0.00%) is below the target coverage (70.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2181      +/-   ##
==========================================
- Coverage   89.85%   89.65%   -0.20%     
==========================================
  Files          73       73              
  Lines       19006    19048      +42     
==========================================
+ Hits        17077    17078       +1     
- Misses       1929     1970      +41     
Flag Coverage Δ
codegenunittests 84.90% <ø> (ø)
nidcpowersystemtests 94.38% <0.00%> (-0.23%) ⬇️
nidcpowerunittests 89.53% <ø> (ø)
nidigitalsystemtests 92.02% <0.00%> (-0.24%) ⬇️
nidigitalunittests 68.44% <ø> (ø)
nidmmsystemtests 92.29% <0.00%> (-0.44%) ⬇️
nifakeunittests 86.01% <ø> (ø)
nifgensystemtests 94.32% <0.00%> (-0.30%) ⬇️
nimodinstsystemtests 73.85% <ø> (ø)
nimodinstunittests 94.20% <ø> (ø)
nirfsgsystemtests 81.12% <ø> (ø)
niscopesystemtests 92.66% <0.00%> (-0.28%) ⬇️
niscopeunittests 43.20% <ø> (ø)
nisesystemtests 91.50% <ø> (ø)
niswitchsystemtests 81.56% <0.00%> (-0.48%) ⬇️
nitclksystemtests 94.87% <ø> (ø)
nitclkunittests 98.26% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...erated/nidcpower/nidcpower/_library_interpreter.py 91.25% <0.00%> (-1.00%) ⬇️
...erated/nidigital/nidigital/_library_interpreter.py 89.66% <0.00%> (-0.72%) ⬇️
generated/nidmm/nidmm/_library_interpreter.py 88.74% <0.00%> (-1.37%) ⬇️
generated/nifgen/nifgen/_library_interpreter.py 95.54% <0.00%> (-1.04%) ⬇️
generated/niscope/niscope/_library_interpreter.py 92.57% <0.00%> (-1.09%) ⬇️
...enerated/niswitch/niswitch/_library_interpreter.py 73.62% <0.00%> (-1.45%) ⬇️

... and 1 file with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ab52ec6...dfb69f2. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@zoechanzy zoechanzy force-pushed the users/zchan/fix-invalid-session-error-description branch from 3c6da41 to ab14a93 Compare June 29, 2026 14:42
Comment on lines +110 to +113
error_string = self.error_message(error_code)
return error_string
except errors.Error:
pass

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It is possible that the session is valid but the returned_error_code unequal to error_code

If this happens, then something likely went wrong retrieving the error message. I'm not sure calling error_message instead of get_error_description is going to change anything in that case.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Calling get_error_description again might cause infinite loop.

@ni-jfitzger ni-jfitzger Jun 30, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

No, I wasn't suggesting that we call get_error_description again. I'm merely trying to think about what behavior we'll see if this new section of code executes.

Oh, i see. Sorry, I meant get_error, not get_error_description.

@jfan-1995 jfan-1995 Jul 2, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I also find this comment a bit confusing....

What I understand is get_error reads what is currently in the session, if it is overwritten or cleared it could still successfully run but will return mismatched error code.

So this try block is to check the error_message in the current session first before proceed to the set_session_handle in the next try block.

@jfan-1995 jfan-1995 Jul 2, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

How about wording the comment to be like this?

# get_error reads the session's error queue, which may have been overwritten,
# causing it to return a mismatched error code. error_message takes the error
# code directly as a parameter and looks up its description without reading the
# queue, so it will just return the description for the specific error code.
# Try it here with the current session before the handle reset in the next block.

Comment thread generated/nidcpower/nidcpower/_library_interpreter.py
@ni-jfitzger

Copy link
Copy Markdown
Collaborator

FYI @zoechanzy , our Linux VM is currently in bad shape, so Linux System tests are going to fail. We hope to have it resolved in the next week or two.

Comment thread build/templates/_library_interpreter.py/_get_error_description.py.mako Outdated
@adrianthongkh

Copy link
Copy Markdown

Requested to update description to be more descriptive of the error.
Provide examples if necessary.

Thanks!

@jfan-1995

jfan-1995 commented Jul 2, 2026

Copy link
Copy Markdown

Propose to rename the title to be Fix the python session to return correct error message when invalid session for MI drivers

In the PR description
List issues fixed by this Pull Request below, if any.

Can you explain with screenshot, something like:

The expected error message when invalidated NI-SWITCH session, the error message is different between Labview and Python

  • Labview error message
image
  • Python error message
image

@zoechanzy zoechanzy changed the title Fix invalid session bad error description Fix the python session to return correct error message when invalid session for MI drivers Jul 2, 2026
Comment thread build/templates/_library_interpreter.py/_get_error_description.py.mako Outdated
@zoechanzy zoechanzy marked this pull request as ready for review July 2, 2026 14:36
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.

6 participants