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 on lines +121 to 122
self.set_session_handle()
error_string = self.error_message(error_code)

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.

Did you add the other error_message call to protect against resetting the session handle in instances where the session is valid but we fail to retrieve the error message?

@zoechanzy zoechanzy Jun 30, 2026

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.

If the scenario is valid session but failed to retrieve error message, it will enter third try block. Resetting the session will not affect its error_code so it will flow into the error_string = self.error_message(error_code) as well. It will still fail to retrieve the error message since the error_code is unchanged. So, it will raise execption and return "Failed to retrieve error description"

If the scenario is invalid session, it will reset the session in third try block and self.error_message(error_code) will not raise execption because session is set to 0 already. So, it will return correct "The session handle is invalid" message.

@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.

If the scenario is valid session but failed to retrieve error message, it will enter third try block.

I'm not sure that's true.
I think the 2nd try block will return from get_error_description with an empty error string.
Have you done testing to determine the behavior?

If you believe we could enter the third try block and reset the session when the session is valid and we fail to retrieve the error message, then you shouldn't be resetting the session. Failure to retrieve an error message does not justify leaking a valid session handle.

@zoechanzy zoechanzy Jul 1, 2026

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.

I think the 2nd try block will return from get_error_description with an empty error string.
Have you done testing to determine the behavior?

I tested with :

  • Unknown error code (-9999999999) → get_error_description returned "Explanation could not be found for the requested status code."
  • VISA layer error code (-1073807339 )→ get_error_description returned "VI_ERROR_TMO: Timeout expired before operation completed."

Since the real driver does not raise errors.Error from error_message() on a valid session either, I used a mock to cover the except errors.Error in second try block and third try block.
with patch.object( type(session._interpreter), 'error_message', side_effect=errors.Error(real_error_code) ): description = session._interpreter.get_error_description(real_error_code)

It returns "Failed to retrieve error description."

Failure to retrieve an error message does not justify leaking a valid session handle.

I will update code to save the session handle before resetting it and restore it afterward to avoid leaking a valid session handle. We cannot selectively restore only valid sessions because we can only determine if a session handle is valid by calling a C driver function and observe whether it succeeds or fails.

@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
(IVI spec requires GetError to fail).
Use error_message instead. It doesn't require a session.
'''
save_vi = self.get_session_handle()

@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.

Hmm you are defining and calling self.get_session_handle() here but reference in the finally. My concern if get_session_handle() raises anything other than errors.Error, it may skip the rest of the try block, and then when run finally block the save_vi is unbound.

I think maybe we should move this save_vi = self.get_session_handle() before the try block?

save_vi = self.get_session_handle()     # moved outside try
try:
   self.set_session_handle()
   error_string = self.error_message(error_code)
   return error_string
except errors.Error:
   pass
finally:
   self.set_session_handle(save_vi)       # always safe or not run if save_vi failed

@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.

Hmm I notice the _library_interpreter.py files on all drivers are also written like this, I not sure how the generated folder files being written, I suppose we only need to change this file and run some local build/script it will update for the _library_interpreter.py files for all drivers?

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.

I suppose we only need to change this file and run some local build/script it will update for the_library_interpreter.py files for all drivers?

@jfan-1995 I updated the py.mako file only and ran tox command to generate all the files and run unit test.

@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