Skip to content

VXI11 lock handling - #633

Merged
MatthieuDartiailh merged 34 commits into
pyvisa:mainfrom
hb020:vxi11-lock-handling
Aug 28, 2026
Merged

VXI11 lock handling#633
MatthieuDartiailh merged 34 commits into
pyvisa:mainfrom
hb020:vxi11-lock-handling

Conversation

@hb020

@hb020 hb020 commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Why

This allows more compliant lock handling on VXI-11.

  • Closes VXI-11 lock handling incorrect #583
  • Executed pre-commit with no errors
  • The change is fully covered by automated unit tests
  • Documented in docs/ as appropriate
  • Added an entry to the CHANGES file

@codecov

codecov Bot commented Aug 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.44304% with 23 lines in your changes missing coverage. Please review.
✅ Project coverage is 48.19%. Comparing base (fde650a) to head (0043e6a).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
pyvisa_py/tcpip.py 64.15% 17 Missing and 2 partials ⚠️
pyvisa_py/attributes.py 90.90% 0 Missing and 1 partial ⚠️
pyvisa_py/gpib.py 0.00% 1 Missing ⚠️
pyvisa_py/prologix.py 0.00% 1 Missing ⚠️
pyvisa_py/testsuite/test_open_timeout.py 96.15% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #633      +/-   ##
==========================================
+ Coverage   44.78%   48.19%   +3.40%     
==========================================
  Files          31       33       +2     
  Lines        5468     5625     +157     
  Branches      534      545      +11     
==========================================
+ Hits         2449     2711     +262     
+ Misses       2987     2865     -122     
- Partials       32       49      +17     
Flag Coverage Δ
unittests 48.19% <85.44%> (+3.40%) ⬆️

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

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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

@hb020
hb020 marked this pull request as draft August 25, 2026 10:05
@hb020

hb020 commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

releasing this for merge. hislip is more effort, will come in a next PR.

@hb020
hb020 marked this pull request as ready for review August 25, 2026 19:08

@MatthieuDartiailh MatthieuDartiailh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

First quick pass.

Comment thread docs/source/faq.rst Outdated
Comment on lines +221 to +222
- Request a lock via ``inst.lock_excl()``. This is the most portable. See above.
- Configure the lock timeout via the PyVISA-Py specific ``rm.visalib.sessions[inst.session].lock_timeout``.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do you need to poke at the internals here ? I would prefer to not encourage such uses.
Since Keysight make use of a non standard attribute to control this parameter, we could do the same. This ways internals are not exposed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

well, it was like that, so I left it. But yes, I prefer the keysight way.

BUT: then we also need to adapt pyvisa, since it is pyvisa that would expose that specific attribute to the user. It is not a standard attribute.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I do not have all the code within easy reach to check but since pyvisa mostly forward directly to the library for get/set attribute we may be able to to define the extra args outside PyVISA.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sure, can be done, the other attributes are 'hard coded' in pyvisa/constants.py. They do not refer to the backends.
Not a big issue, but then we'll have to make sure to catch any mismatching package versions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'll work on something. For now, only in pyvisa-py. And making sure it can live with outdated pyvisa.

Comment thread pyvisa_py/tcpip.py Outdated
Comment on lines +892 to +898
# TODO determine how OP_FLAG_WAIT_BLOCK could be set from the outside.
# VPP-4.3 does not provide a clear definition for it.
# For now, we derive it from self.lock_timeout.
lock_timeout = self.lock_timeout
if lock_timeout != constants.VI_TMO_IMMEDIATE:
flags |= vxi11.OP_FLAG_WAIT_BLOCK

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This could be factored out in a helper.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes, will do

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

@MatthieuDartiailh MatthieuDartiailh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would expect changes to be required for serial and USB for locking when opening. And for those and GPIB since locking does not do anything we should reject non default value.

@hb020

hb020 commented Aug 26, 2026

Copy link
Copy Markdown
Contributor Author

Implemented VI_KTATTR_LOCKWAIT, removed lock_timeout. It is all limited it to vxi-11

@hb020

hb020 commented Aug 26, 2026

Copy link
Copy Markdown
Contributor Author

In fact, VPP-4.3 states that all types should support locking, both exclusive locks and shared locks, AND nested locking.

For exclusive locks, VXI-11 and HiSLIP instruments handle that themselves (maybe also vicp and vxi?).
The other instrument interfaces do not support it AFAIK.
Also, shared locking, and nested locking, mean a fair amount of work inside the higher layers of pyvisa-py. Much more than this. Seen potential low return (those who use shared locking, often use large setups, and they use NI-VISA), not sure I will get to that one day.

@hb020

hb020 commented Aug 26, 2026

Copy link
Copy Markdown
Contributor Author

Now I need to work on the timeout of the remote/local functions

@MatthieuDartiailh

Copy link
Copy Markdown
Member

I would expect changes to be required for serial and USB for locking when opening. And for those and GPIB since locking does not do anything we should reject non default value.

I realize this was silly since serial is by definition a unique connection and USBTMC and GPIB are likely the same. I will make another more thorough review but I think are nearly good.

@hb020

hb020 commented Aug 26, 2026

Copy link
Copy Markdown
Contributor Author

merged.
Preferred order of integration: this PR and only then #626. I might need to do some merging and cleaning for that one, and it is less important.

@MatthieuDartiailh MatthieuDartiailh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Some small comments

Comment thread pyvisa_py/tcpip.py Outdated
flags = 0
flags, lock_timeout = self._adapt_flags_and_lock_timeout(flags)

# XXX make this nicer (either validate protocol or pass it)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not sure this comment still applies

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

corrected comment

Comment thread docs/source/faq.rst Outdated
Comment on lines +231 to +246
``VI_KTATTR_LOCKWAIT`` may not be available yet in pyvisa. In that case, you could do this:

>>> import pyvisa
>>> rm = pyvisa.ResourceManager('@py')
>>> inst = rm.open_resource('TCPIP::192.168.1.100::INSTR')
>>>
>>> # Define the raw hex constant for VI_KTATTR_LOCKWAIT
>>> VI_KTATTR_LOCKWAIT = 0x0FFF002BL
>>>
>>> # Set lockwait to True (VI_TRUE = 1)
>>> inst.set_attribute(VI_KTATTR_LOCKWAIT, 1)
>>> # Read back the attribute value
>>> lockwait_val = inst.get_attribute(VI_KTATTR_LOCKWAIT)
>>> print("Lockwait state:", lockwait_val)
>>>
>>> # Do your operations

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do you need this since we force set this attribute?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Otherwise you get this: NameError: name 'VI_KTATTR_LOCKWAIT' is not defined

It needs to be added to pyvisa.constants

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

clarified faq

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

oops. you're right. WIll correct the faq.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

corrected

Comment thread pyvisa_py/tcpip.py Outdated
Comment on lines +527 to +535
#: Time to wait in ms before erroring with a timeout when trying to acquire a lock
lock_timeout: int = 10000
# 0 = immediate, >0 = wait for that many milliseconds
# This is used for most operations (mainly except open_resource and lock_excl)
#
# NI-VISA and R&S VISA do not expose any of this.
# Keysight exposes the Keysight-specific VISA ViBoolean local (per-session) attribute VI_KTATTR_LOCKWAIT
# When False, when already locked, immediately returns VI_ERROR_RSRC_LOCKED
# When True, uses lock timeout = session timeout interval. When locked and timed out, returns VI_ERROR_TMO
# lock_timeout: int = 0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This whole paragraph seems out of place.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes, will remove the comments

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

@MatthieuDartiailh
MatthieuDartiailh merged commit 328408f into pyvisa:main Aug 28, 2026
18 checks passed
@hb020
hb020 deleted the vxi11-lock-handling branch August 28, 2026 20:17
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.

VXI-11 lock handling incorrect

2 participants