Skip to content

feat: expose thread API for Go-based PHP extensions#2305

Open
johanjanssens wants to merge 2 commits intophp:mainfrom
johanjanssens:feat/thread-api
Open

feat: expose thread API for Go-based PHP extensions#2305
johanjanssens wants to merge 2 commits intophp:mainfrom
johanjanssens:feat/thread-api

Conversation

@johanjanssens
Copy link
Copy Markdown

Add APIs that enable Go-based PHP extensions to execute on existing FrankenPHP threads and access per-request state:

  • Thread(index): retrieves a PHP thread by its index, returning its *http.Request which carries the request context
  • PHPThread.Pin(): pins a Go object to prevent GC collection
  • PHPThread.IsRequestDone(): checks if the request has been closed
  • frankenphp_thread_index(): C function returning the current thread index so C extension methods can call back into Go

This makes it possible to build PHP extensions where PHP calls into C, C resolves the current thread index, and Go handles the logic with full access to the request context.

Add APIs that enable Go-based PHP extensions to execute on existing
FrankenPHP threads and access per-request state:

- Thread(index) — retrieves a PHP thread by its index, returning its
  *http.Request which carries the request context
- PHPThread.Pin() — pins a Go object to prevent GC collection
- PHPThread.IsRequestDone() — checks if the request has been closed
- frankenphp_thread_index() — C function returning the current thread
  index so C extension methods can call back into Go

This makes it possible to build PHP extensions where PHP calls into C,
C resolves the current thread index, and Go handles the logic with
full access to the request context.
@johanjanssens
Copy link
Copy Markdown
Author

@dunglas These are the key changes required for:

Copy link
Copy Markdown
Member

@dunglas dunglas left a comment

Choose a reason for hiding this comment

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

LGTM, but could you add some tests please?

}

// PHPThread exposes a PHP thread's request context.
type PHPThread struct {
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.

Maybe we could use this new type internally too, for code clarity?

Copy link
Copy Markdown
Author

@johanjanssens johanjanssens Mar 26, 2026

Choose a reason for hiding this comment

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

Agreed. Kept it minimal to avoid a large refactor.

We can definitely take this further. Need to define scope a bit. For example, if you push this deeper I would suggest to consider addressing worker context, at the moment a worker carries a dummy context at boot time.

There's no way to inject persistent context from outside (e.g. middleware-style state that lives across requests on a worker thread). Could look into making PHPThread carry context consistently for both regular and worker threads.

Happy to expand this PR, or follow up in a new one. A context refactor would touch quite a bit more code.

Test Thread(), PHPThread.Pin(), and request handling. Fix nil pointer
dereference in Thread() when no request is active or index is out of
bounds.
@johanjanssens
Copy link
Copy Markdown
Author

Tests added.

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.

2 participants