GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GH-93503: Add thread-specific APIs to set profiling and tracing functions in the C-API by pablogsal · Pull Request #93504 · python/cpython

Conversation

@pablogsal

@pablogsal pablogsal commented Jun 5, 2022 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

closes: #93503

  • Issue: Add public APIs to set trace and profile function in other threads. #93503

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@pablogsal
pablogsal requested a review from markshannon as a code owner June 5, 2022 00:24
@bedevere-bot bedevere-bot added the awaiting core review label Jun 5, 2022
@pablogsal pablogsal changed the title GH-93503 Add thread-specific APIs to set profiling and tracing functions in the C-API GH-93503: Add thread-specific APIs to set profiling and tracing functions in the C-API Jun 5, 2022
@pablogsal
pablogsal requested a review from vstinner June 5, 2022 00:26
vstinner
vstinner reviewed Jun 6, 2022
View reviewed changes
Comment thread Doc/c-api/init.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/ceval.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/ceval.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/ceval.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@markshannon

markshannon commented Jun 6, 2022

Copy link
Copy Markdown
Member

I think this is too error prone, for a couple of reasons.

  • The main reason is that this sets the expectation that C-API functions taking PyThreadState can take any thread state.
    However, most C-API functions that take PyThreadState are likely to leave the VM in an invalid state if passed anything other than the current thread state.
  • What happens when the thread state belongs to a different sub-interpreter from obj?

To be fair, this isn't just an issue with these functions, but any function that takes a PyThreadState parameter.

#93503 is about adding an API to set tracing/profiling on all threads.
Why not add an API that does that?
void PyEval_SetThreadProfileAllThreads(Py_tracefunc func, PyObject *obj)

Also, I don't like offering operations in the C-API that aren't available in Python. We shouldn't be forcing people to write C.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal

pablogsal commented Jun 6, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

Why not add an API that does that?

I am fine with that. Do you want me to update this PR or create another one?

Also, I don't like offering operations in the C-API that aren't available in Python. We shouldn't be forcing people to write C.

That ship already sailed. Do mean that you would like to also expose such an API in the Python layer?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Jun 6, 2022

Copy link
Copy Markdown
Member

The threading module has setprofile() and settrace() functions.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal

pablogsal commented Jun 6, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

The threading module has setprofile() and settrace() functions.

But those only work for newly created threads as I noted in the issue. They are used only when new threads are bootstrapped. What we are trying to do is to add APIs that affect all current threads.

We also cannot change those for backwards compatibility, but we could place the new APIs in the thread module for Python access.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal

pablogsal commented Jun 6, 2022

Copy link
Copy Markdown
Member Author

@markshannon you ok with this plan?

  • Changing the proposed C APIs to set the trace/profile functions in all threads.
  • Add a python API in the thread module that calls the C API.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@markshannon

markshannon commented Jun 6, 2022

Copy link
Copy Markdown
Member

@markshannon you ok with this plan?

* Changing the proposed C APIs to set the trace/profile functions in all threads.

* Add a python API in the thread module that calls the C API.

Yes. Sounds good to me.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Jun 6, 2022

Copy link
Copy Markdown
Member

But those only work for newly created threads as I noted in the issue. They are used only when new threads are bootstrapped. What we are trying to do is to add APIs that affect all current threads.

Would it make sense to add a parameter to these threading functions or to sys functions to apply the new tracing/profiling functions to existing threads?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal

pablogsal commented Jun 6, 2022

Copy link
Copy Markdown
Member Author

I am ambivalent, but the new parameter sounds good if you prefer it that way.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal
pablogsal force-pushed the gh-93503 branch from e13c19e to 1c2fb52 Compare June 6, 2022 15:05
@pablogsal

pablogsal commented Jun 6, 2022

Copy link
Copy Markdown
Member Author

Ok, I have made a first version of the idea proposed by @markshannon. Please @vstinner @markshannon review again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

markshannon
markshannon reviewed Jun 6, 2022
View reviewed changes
Comment thread Doc/library/threading.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

vstinner
vstinner reviewed Jun 6, 2022
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I suggest all_threads=True rather than running_threads=True: in your implementation, threading.settrace() changes old and new threads, all threads, not only old ("running") threads.

With the PR, we have:

  • sys.settrace(): current thread
  • threading.settrace(): new threads
  • threading.settrace(all_threads=True): all threads (old, new and current)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/library/threading.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal

pablogsal commented Aug 1, 2022

Copy link
Copy Markdown
Member Author

@vstinner I'm going to change this PR to follow @markshannon request and make it a separate function in Python instead of a parameter.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Aug 3, 2022

Copy link
Copy Markdown
Member

@vstinner I'm going to change this PR to follow @markshannon request and make it a separate function in Python instead of a parameter.

Sure, as you want. What would be the API in that case?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal
pablogsal force-pushed the gh-93503 branch from 1c2fb52 to 6ecc7b1 Compare August 4, 2022 14:11
@pablogsal
pablogsal requested review from markshannon and vstinner August 4, 2022 14:13
@pablogsal

pablogsal commented Aug 4, 2022

Copy link
Copy Markdown
Member Author

I have updated the APIs to use separate functions.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal

pablogsal commented Aug 4, 2022

Copy link
Copy Markdown
Member Author

What would be the API in that case?

threading.settrace_all_threads and threading.setprofile_all_threads

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

vstinner
vstinner reviewed Aug 4, 2022
View reviewed changes
Comment thread Python/ceval.c
}
}

void

@vstinner vstinner Aug 4, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

It's not great that the function cannot report errors. While not returning -1 on error and pass the exception to the caller, rather than handling it with _PyErr_WriteUnraisableMsg()?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@pablogsal pablogsal Aug 4, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I am mimicking what we do already in PyEval_SetProfile and to avoid surprises I think we should keep these two as synchronized as possible.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Aug 4, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

We cannot fix the API of old functions, but we can avoid past mistakes in newly added functions.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Aug 4, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

If you want to ignore exceptions on purpose, please mention it in the function documentation.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@pablogsal pablogsal Aug 4, 2022

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

If you want to ignore exceptions on purpose, please mention it in the function documentation.

Yeah, I want to do that because I don't want to stop setting the profile function on other threads if an exception in one fails. Unless you strongly disagree, I will document this.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Python/ceval.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal
pablogsal requested a review from vstinner August 4, 2022 15:19
@pablogsal
pablogsal force-pushed the gh-93503 branch from 2efeb91 to 3e40135 Compare August 4, 2022 15:30
vstinner
vstinner reviewed Aug 5, 2022
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Honestly, I don't know if HEAD_LOCK/HEAD_UNLOCK is needed, I'm not sure if the GIL protects us here. The PyThreadState_Next() API is not well documented for thread safety :-(

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Python/sysmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal
pablogsal requested a review from vstinner August 23, 2022 18:52
@pablogsal

pablogsal commented Aug 24, 2022

Copy link
Copy Markdown
Member Author

I plan to land this this week or the next one to avoid merge conflicts. @markshannon can you give it a last pass?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

markshannon
markshannon reviewed Aug 24, 2022
View reviewed changes

@markshannon markshannon left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

One minor issue with the docs, otherwise LGTM.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/c-api/init.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

pablogsal added 7 commits August 24, 2022 21:34
@pablogsal
pythongh-93503: Add APIs to set profiling and tracing functions in al…
10d7746
…l threads in the C-API
@pablogsal
Use a separate API
b3a223f
@pablogsal
Fix NEWS entry
438eae6
@pablogsal
Add locks around the loop
5cbcaa1
@pablogsal
Document ignoring exceptions
b3333d8
@pablogsal
Use the new APIs in the sys module
d580e5d
@pablogsal
Update docs
05bf74c
@pablogsal
pablogsal force-pushed the gh-93503 branch from 34b10ee to 05bf74c Compare August 24, 2022 20:36
@pablogsal
pablogsal merged commit e34c82a into python:main Aug 24, 2022
@pablogsal
pablogsal deleted the gh-93503 branch August 24, 2022 22:21
@bedevere-bot bedevere-bot removed the awaiting core review label Aug 24, 2022
@vstinner

vstinner commented Sep 8, 2022

Copy link
Copy Markdown
Member

Nice enhancement, thanks.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Sep 8, 2022

Copy link
Copy Markdown
Member

You should document the newly added C API functions to: https://docs.python.org/dev/whatsnew/3.12.html#c-api-changes

You should also document the new threading functions in the same document, in a threading section, near: https://docs.python.org/dev/whatsnew/3.12.html#improved-modules

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal

pablogsal commented Sep 8, 2022

Copy link
Copy Markdown
Member Author

You should document the newly added C API functions to: docs.python.org/dev/whatsnew/3.12.html#c-api-changes

You should also document the new threading functions in the same document, in a threading section, near: docs.python.org/dev/whatsnew/3.12.html#improved-modules

#96681

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@markshannon markshannon markshannon left review comments
@vstinner vstinner Awaiting requested review from vstinner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Add public APIs to set trace and profile function in other threads.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@pablogsal @markshannon @vstinner @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.