TELunus
I seem to be having an issue making some code asynchronous. I’m running
stream = Task.Supervisor.async_stream_nolink(
Module.TaskSupervisor,
enum,
fn (item) -> my_func(item, 2) end,
[ordered: false, max_concurrency: 1]
)
results = Enum.to_list(stream)
which gives me 12:31:22.290 [error] Postgrex.Protocol (#PID<0.678.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.1768.0> exited while client #PID<0.1771.0> is still running with: shutdown
part of the code that’s run by my_func is an ecto call
IO.puts("before")
Repo.get(schema, id)
IO.puts("after")
and I’m seeing the "before" get printed right before the error message, but I’m not seeing the "after"
On the other hand if I change my code to run synchronously as
stream = Enum.map(enum, fn (item) -> my_func(item, 2) end)
results = Enum.to_list(stream)
then everything works fine. There’s no error message, and both print statements get run.
Any thoughts on what I could be doing wrong or how I can fix this?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #ai
- #graphql
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
alco
Are you running this code in tests with
Ecto.Adapters.SQL.Sandbox?TELunus
I’m running from
mix testwithin my
config/test.exsso I take it I am.alco
You can either use
:sharedmode withEcto.Adapters.SQL.Sandboxand disable async tests for your test module or explicitly transfer the Ecto connection to the spawned task process. See Ecto.Adapters.SQL.Sandbox — Ecto SQL v3.14.0 for more details.TELunus
It looks like the project is already doing that. The test file starts with
And in
model_case.exwe’ve gotSo since
:asyncisn’t included in theuseline I believe that means we’re using shared mode, so any process should be able to connect to the DB connection.TELunus
I’m not sure how relevant it is, but while searching for
Adapters.SQL.Sandboxin the project I discovered that it shows up intest/test_helpers.ex:Which seems to set the mode without setting
:sharedalco
Try spawning your tasks with
Task.async_stream. If that works, then I think the problem is that in your original code task processes are started by a supervisor process and that’s why they don’t see the DB connection checked out by the test process. Performing a manual allowance in the task process should work.alco
There’s another option now: update your deps and it should just work - Plataformatec on X: "We have just released DBConnection v2.0.4 (which powers Ecto) to use Elixir's v1.8 task callers feature. This means using the SQL Sandbox to test code that uses tasks for concurrency should work out of the box - and it should work concurrently too (wow, such concurrency)!" / X
TELunus
Thanks for the suggestions. I just tried with
async_stream:then with a manual
allow:as well as with both:
all unfortunately to no noticeable effect:
14:12:25.494 [error] Postgrex.Protocol (#PID<0.658.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.1745.0> exited while client #PID<0.1748.0> is still running with: shutdownAs for the dependency update route, that looks like a great idea. Alas we’re still on Elixir 1.6 in this project, and I think it would be a fairly large undertaking to get everything working under 1.8. I think we should probably do it at some point, but I don’t think now is the time.
TELunus
Oops, it looks like I got your suggestion wrong, and had switched from
Task.Supervisor.async_stream_nolinktoTask.Supervisor.async_streaminstead ofTask.async_stream.I’ve now tried switching to
Task.async_streamboth with a manual allow:and without:
And again unfortunately all to no noticeable effect:
19:08:14.135 [error] Postgrex.Protocol (#PID<0.658.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.1748.0> exited while client #PID<0.1751.0> is still running with: shutdownalco
I’ve realized that I misdiagnosed your problem.
When I tried to reproduce the error you’re getting, I realized that I had been thinking of the other error that occurs when a process cannot find a checked out connection. It looks like this:
However, the error you’re getting must be caused by the test process exiting before another process stopped using the DB connection. I was only able to reproduce this as follows:
The full error I’m getting looks a bit different from yours, but I think it happen in a similar circumstance:
In the end, you were right about your tests running with
async: falsesince that the default, so my analysis was wrong in that regard. However, I think there’s something else going on in your test suite outside of the code snippets you’ve shared so far.