Skip to content

Sandbox: Allow {:late, id_tuple} - #762

Open
mnussbaumer wants to merge 1 commit into
elixir-ecto:masterfrom
mnussbaumer:late_allowances
Open

mnussbaumer wants to merge 1 commit into
elixir-ecto:masterfrom
mnussbaumer:late_allowances

Conversation

@mnussbaumer

@mnussbaumer mnussbaumer commented Nov 30, 2026 •

Copy link
Copy Markdown

Ecto lacks a way of setting allowances for testing with late bindings, or in other words, allowing an "addressable" process name be used instead of the definitive pid the process is going to have. In part this needs to touch also db_connection internals because part of the logic for the sandbox is there, due to the way it's architected.

Late allowances are useful for tests that you want asynchronous but for which you can't know the pid at test start, or processes that use the database on their own initial setup (init etc). For solving this you need to either use other sandbox mode and not use async tests or fiddle with your process code to allow special calls and cases during init depending on environment, either way, it always pushes the tests further from testing the real implementation of what's going to run in production.

The changes are minimal and they require specific formats so they should conflict with anything else. There would be ways of making this more readable by using the state of the processes currently being used for manager in db_connection, but that would be a bigger change. The API for a late binding allowance becomes:

Ecto.Adapters.SQL.Sandbox.allow(
      DB.Repo,
      self(),
      {:late, Statems.User.statem_global_id(player.id)}
)

For instance. There's a penalty paid when retrieving the real PID but this path only runs when the sandbox is in :manual, and there was no match. That path already raises today, so the cost, given it's probably only used for test contexts is near 0.

The db_connection PR is elixir-ecto/db_connection#364 (comment)

In total it's 10 lines here and 32 in db_connection.

…ip pool - the db_connection library needs to then be able to figure out when a :late binding is set
@josevalim

Copy link
Copy Markdown
Member

If we are going ahead with this, we definitely need more documentation, but another way to deal with this is for the message to set its $callers or $ancestors to something ownership can resolve. I wonder if nimble_ownership has a similar mechanism we can borrow.

@mnussbaumer

Copy link
Copy Markdown
Author

If we are going ahead with this, we definitely need more documentation, but another way to deal with this is for the message to set its $callers or $ancestors to something ownership can resolve. I wonder if nimble_ownership has a similar mechanism we can borrow.

I would be fine with writing more detailed documentation including a section specific for it and possibly tests. The issue with ancestors is that you still need to set them and my usual case the start is under a dynamic supervisor. There's probably ways to weave that into the started processes, but it seems like it might be much more convoluted? Anyway, I personally always felt like there should be a way to address these situations with allowances (and to an extent in Mox expectations too...) in the sense that: I know the name of the process, I know it's going to do something, I explicitly want to give it an allowance ahead of time but I can't because PIDs only exist after a process is started. I'm not too happy with the split between this and db_connection but I also don't see a way it could happen otherwise since the sandbox send the calls directly to the db_connection manager, not touching the paths here anymore.

@josevalim

Copy link
Copy Markdown
Member

I believe Mox added support for anonymous functions, instead of {:late, ...}, which allows you to execute any code lazily. Which is why I suggested for us to check and mirror there.

@mnussbaumer

Copy link
Copy Markdown
Author

This would mean that the fun/0 allowance would be resolved in db_connection right? Because here in this case, ecto_sql's sandbox only "registers" the "allowance" with the db_connection pool manager, it ends up being the pool manager that does the resolution as the checkout requests are done through the test process right? The test process checks out the connection and is no longer involved in the allowance negotiation? At least when tracing things this was what I ended up at in my understanding. With mox by what I understood it's always the mox process that evaluates the allowance at call time?

To use a function it would mean introducing nimble_ownership for this case in the db_connection manager - I think this requires nimbleownership to be started and then for it to manage the pid allowances itself. So perhaps it would make more sense if it was to gradually migrate the sandbox model to use nimbleownership? Or you mean to change {:late, addressable_identifier()} for {:late, fun/0} and simply resolve it at call time?

What I like in my proposal is that it's still just using native ways of addressing processes, meaning {:global | :local, identifier()} (perhaps {:via ...} should also be supported given this is elixir) tuples that then use the regular named process finding. It's currently not replacing the allowances by the pid once it resolves it but I wasn't sure it made sense - it's less strict this way, since a process can die and be brought up under a different pid and still be allowed, at the same time if that's happening with named processes and isn't expected you probably have an issue somewhere else that needs addressing first. Nonetheless all that can be made so.

I'm open to exploring other options but I have not used nimbleownership so not entirely sure what would need to happen to implement it here, in defence of the fun/0 captures, they do solve other cases where the resolution isn't by traditional process name registration - for those cases {:via, _} support and usage of proper naming/retrieval behaviour from the "user" would be the ideal, but funs do solve the issue without fuss and no limitations so perhaps they're the "solves most cases" case that should be implemented.

@josevalim

Copy link
Copy Markdown
Member

No the plan is not to use nimble ownership, just to mirror its API for consistency. But there may be differences between them indeed, such as where it is resolved.

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