Skip to content

Regroup all session creation in util get_localAPI_session or APISession class. - #129

Open
Ythogtha wants to merge 9 commits into
3.2.12-8.3-develfrom
agf-xapi_session_refactoring
Open

Regroup all session creation in util get_localAPI_session or APISession class.#129
Ythogtha wants to merge 9 commits into
3.2.12-8.3-develfrom
agf-xapi_session_refactoring

Conversation

@Ythogtha

Copy link
Copy Markdown

New APISession class to handle lifecycle of the session, with context, or atexit handlers.
Use various originator for xapi session creation, to differentiate where they are being created.

APISession uses get_localAPI_session() for session creation, so there is only one place for session creation, using xenapi.login_with_password.
get_localAPI_session should be avoided and only remain in lcache.py.

The session originator is shown on all logs messages pertaining to sessions, in SMlog, in the format APISession [{originator}] {message}.
All login and logout messages starts with login or logout hence it is possible to count them to check if there is session leakage :

$ echo "login: $(grep -e 'APISession \[.*\] login' SMlog* | wc -l), logout: $(grep -e 'APISession \[.*\] logout' SMlog* | wc -l)"
login: 8024, logout: 8024

@Ythogtha
Ythogtha force-pushed the agf-xapi_session_refactoring branch 12 times, most recently from 0f82c30 to be646e3 Compare April 2, 2026 05:09
@Ythogtha
Ythogtha requested review from Nambrok and Wescoeur April 2, 2026 05:10
@Ythogtha
Ythogtha force-pushed the agf-xapi_session_refactoring branch 3 times, most recently from e1507b2 to c0658d5 Compare April 2, 2026 13:45
@Ythogtha
Ythogtha force-pushed the agf-xapi_session_refactoring branch from eae5e31 to cee2b15 Compare April 21, 2026 09:35
@Wescoeur
Wescoeur requested a review from a team April 29, 2026 13:40
@Wescoeur
Wescoeur force-pushed the 3.2.12-8.3 branch 4 times, most recently from bc30ef6 to 1dfb701 Compare May 19, 2026 17:17
@Ythogtha
Ythogtha force-pushed the agf-xapi_session_refactoring branch from cee2b15 to 4a333e5 Compare May 20, 2026 16:16
Comment thread drivers/util.py Outdated
Comment thread drivers/util.py
Comment thread drivers/util.py Outdated
Comment thread drivers/lcache.py Outdated
@Ythogtha
Ythogtha requested a review from Millefeuille42 May 24, 2026 16:12

@Millefeuille42 Millefeuille42 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All good for me except the remaining import util

Comment thread drivers/lcache.py Outdated
@Ythogtha
Ythogtha force-pushed the agf-xapi_session_refactoring branch 2 times, most recently from bd3737d to 119cae3 Compare May 26, 2026 09:46
Comment thread drivers/cleanup.py Outdated
Comment thread drivers/cleanup.py Outdated
Comment thread drivers/coalesce-leaf Outdated
Comment thread drivers/cleanup.py Outdated
Comment thread drivers/lcache.py Outdated
Comment thread drivers/util.py Outdated
Comment thread drivers/util.py Outdated
Comment thread drivers/util.py Outdated
Comment thread drivers/util.py Outdated
Comment thread drivers/util.py
@Nambrok
Nambrok force-pushed the 3.2.12-8.3-devel branch from e712aa5 to fce2d13 Compare June 29, 2026 07:36
@Ythogtha
Ythogtha force-pushed the agf-xapi_session_refactoring branch from 8173b33 to 3c34049 Compare June 29, 2026 14:10
@AnthoineB
AnthoineB requested a review from a team July 2, 2026 13:04
@Nambrok
Nambrok force-pushed the 3.2.12-8.3-devel branch from 6607beb to c4dce9e Compare July 2, 2026 14:25
@Ythogtha
Ythogtha force-pushed the agf-xapi_session_refactoring branch from 3c34049 to 95a0211 Compare July 7, 2026 10:00
Comment thread drivers/linstorcowutil.py Outdated
Comment thread drivers/mpathcount.py Outdated
Comment thread drivers/mpathcount.py
Comment thread drivers/blktap2.py Outdated
Comment thread drivers/resetvdis.py Outdated
Comment thread drivers/cleanup.py Outdated
Comment thread drivers/cleanup.py Outdated
Comment thread drivers/cleanup.py Outdated
Comment thread drivers/util.py Outdated
Comment thread drivers/util.py Outdated
@Ythogtha
Ythogtha force-pushed the agf-xapi_session_refactoring branch from fb254f1 to c4d891b Compare July 30, 2026 13:10
@Ythogtha
Ythogtha requested a review from Wescoeur July 30, 2026 15:37
@Wescoeur
Wescoeur requested a review from Millefeuille42 August 3, 2026 14:31

@Millefeuille42 Millefeuille42 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

OK for me if and when all pending comments are resolved.

Comment thread drivers/linstorcowutil.py Outdated
Comment thread drivers/util.py
Comment on lines +776 to +778
class ApiSession(contextlib.AbstractContextManager):
session=None

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thinking about it, could it be interesting to define/override __call__ to login and return session? This would avoid the long and redundant api_session.session and reduce it to api_session().

Not sure if it's very clear and explicit when reading the code thought.

This is a non blocking comment, only putting it here to start a discussion.

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 guess we can have a card about that.

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.

The problem being that we need to keep a reference to the ApiSession object. If we keep only the session object, the ApiSession instance is destroyed, and the session closed.
And that is the whole point of this PR: make sure sessions are closed.

So we'll keep having an ApiSession object, and use api_session.session...

I'm open to other ideas, but here I tried not to alter, or wrap, the session object, and compose with all the preexisting different methods used for opening and not-always-closing sessions.

Hence, for now, I'd prefer to keep it that way.
It'll be easier later to harmonize ApiSession usages, and simplify it.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

My comment wasn't about removing the ApiSession object, but rather overriding the __call__ method. Which won't affect the lifetime of the object and rather simplify it's usage.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thinking about it, overriding __call__ can be hard to read and can introduce issues when auto mocking.

What we can do instead is to rename the class to something that doesn't end in session like XapiSessionManager. Since the object itself doesn't represent the session but a manager over the said object.

Comment thread drivers/XE_SR_ERRORCODES.xml Outdated
Comment thread drivers/cleanup.py Outdated
Comment thread drivers/blktap2.py Outdated
Comment thread drivers/util.py
Comment on lines +776 to +778
class ApiSession(contextlib.AbstractContextManager):
session=None

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 guess we can have a card about that.

@Nambrok
Nambrok force-pushed the 3.2.12-8.3-devel branch 2 times, most recently from 1825cbe to 9978d7f Compare August 13, 2026 07:45
@Ythogtha
Ythogtha requested a review from Wescoeur August 13, 2026 09:12
@Ythogtha
Ythogtha force-pushed the agf-xapi_session_refactoring branch from b4dd99a to 30447d3 Compare August 13, 2026 09:32
Comment thread drivers/util.py

@override
def __exit__(self, _type, _value, _traceback):
self.logout(log=f"logout exception[{_type}] {_value}" if _type else "logout context")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Maybe refine the log messages to make complete sentences like logout on context exit and logout on exception

@Millefeuille42 Millefeuille42 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

OK for me. Not a fan of having a lot of api_session.session, hence the suggestion maybe to rename the class. Ok for me to skip this / create a card for it if that's not's a blocker for the other reviewers.

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

Code must be rebased before merge, otherwise nothing to add, perfect!

@Wescoeur

Wescoeur commented Sep 2, 2026

Copy link
Copy Markdown
Member

I will create a kanban card regarding the point raised by @Millefeuille42.

…on class.

New APISession class to handle lifecycle of the session, with context, or atexit handlers.
Use various originator for xapi session creation, to differentiate where they are being created.

Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
Do not SMlog information when __del__ is called and the session has already been closed.

Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
… are set.

SMlogs are set to LOG_DEBUG, except on failure to create a session.
Fixed some less readable code:
- lcache.py: import util on top instead of inside a method
- util.py; removed double tempfile import

Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
…out calls, privatise some APISession methods.

Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
Also fix some strings.

Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
…in cleanup.py

Due to the rebase, some old-style session management appeared, this commit makes them use the new APISession.

Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
Reworked some APISession originator for better consistency.
Removed unuseful `session` intermediate variables.
Reworked restevdis to avoid a huge indentation caused by APISession, by using a function.
Adds a try/finally for session.logout() in util.py#sr_get_capability().

Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
Signed-off-by: Arnaud Garcia-Fernandez <arnaud.garcia-fernandez@vates.tech>
@Ythogtha
Ythogtha force-pushed the agf-xapi_session_refactoring branch from 30447d3 to b813746 Compare September 3, 2026 08:34
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.

3 participants