Repository: faust-streaming/faust (Python)

Issue:
ValueError: The future belongs to a different loop than the one specified as the loop argument
## Checklist

- [x] I have included information about relevant versions
- [ ] I have verified that the issue persists when using the `master` branch of Faust.

## Steps to reproduce

Create a fastapi service and run faust on startup in the same loop. Use uvloop in uvicorn

## Expected behavior

Services should start without issues

## Actual behavior

Some services like Producer raise an error that they are attached to a different loop.

## Full traceback

```pytb
future: <Task finished name='Task-4' coro=<Service.start() done, defined at .../lib/python3.10/site-packages/mode/services.py:799> exception=AssertionError('Please create objects with the same loop as running with')>   call_trace=/Users/gellertd/.pyenv/versions/3.10.6/lib/python3.10/asyncio/runners.py L44
Traceback (most recent call last):
  File .../lib/python3.10/site-packages/mode/services.py", line 800, in start
    await self._default_start()
  File ".../lib/python3.10/site-packages/mode/services.py", line 807, in _default_start
    await self._actually_start()
  File ".../lib/python3.10/site-packages/mode/services.py", line 831, in _actually_start
    await child.maybe_start()
  File ".../lib/python3.10/site-packages/mode/services.py", line 859, in maybe_start
    await self.start()
  File ".../lib/python3.10/site-packages/mode/services.py", line 800, in start
    await self._default_start()
  File ".../lib/python3.10/site-packages/mode/services.py", line 807, in _default_start
    await self._actually_start()
  File ".../lib/python3.10/site-packages/mode/services.py", line 824, in _actually_start
    await self.on_start()
  File ".../lib/python3.10/site-packages/faust/transport/drivers/aiokafka.py", line 1301, in on_start
    await producer.start()
  File ".../lib/python3.10/site-packages/aiokafka/producer/producer.py", line 345, in start
    assert (
AssertionError: Please create objects with the same loop as running with```

# Versions

* Python version: 3.10.6
* Faust version: >=0.10.0
* Operating system: MacOS 13.1 (22C65)




Fix (patch):
diff --git a/faust/app/base.py b/faust/app/base.py
index 0a5bb310c..bdf4cc529 100644
--- a/faust/app/base.py
+++ b/faust/app/base.py
@@ -466,7 +466,7 @@ def __init__(
         self._default_options = (id, options)
 
         # The agent manager manages all agents.
-        self.agents = AgentManager(self)
+        self.agents = AgentManager(self, loop=loop)
 
         # Sensors monitor Faust using a standard sensor API.
         self.sensors = SensorDelegate(self)
@@ -1791,7 +1791,7 @@ def _update_assignment(self, assigned: Set[TP]) -> Tuple[Set[TP], Set[TP]]:
         return revoked, newly_assigned
 
     def _new_producer(self) -> ProducerT:
-        return self.transport.create_producer(beacon=self.beacon)
+        return self.transport.create_producer(loop=self.loop, beacon=self.beacon)
 
     def _new_consumer(self) -> ConsumerT:
         return self.transport.create_consumer(
diff --git a/faust/transport/consumer.py b/faust/transport/consumer.py
index 9987db910..b4f13e8b4 100644
--- a/faust/transport/consumer.py
+++ b/faust/transport/consumer.py
@@ -485,7 +485,7 @@ def __init__(
         self.not_waiting_next_records = Event()
         self.not_waiting_next_records.set()
         self._reset_state()
-        super().__init__(**kwargs)
+        super().__init__(loop=loop, **kwargs)
         self.transactions = self.transport.create_transaction_manager(
             consumer=self,
             producer=self.app.producer,
diff --git a/faust/transport/drivers/aiokafka.py b/faust/transport/drivers/aiokafka.py
index d8043d2f8..9c94d7c35 100644
--- a/faust/transport/drivers/aiokafka.py
+++ b/faust/transport/drivers/aiokafka.py
@@ -1482,7 +1482,7 @@ async def _create_topic(
                 topic,
                 partitions,
                 replication,
-                loop=asyncio.get_event_loop_policy().get_event_loop(),
+                loop=self.loop,
                 **kwargs,
             )
         try:
diff --git a/faust/transport/producer.py b/faust/transport/producer.py
index 7e1a040fc..dbcab255d 100644
--- a/faust/transport/producer.py
+++ b/faust/transport/producer.py
@@ -144,7 +144,7 @@ def __init__(
         self.partitioner = conf.producer_partitioner
         api_version = self._api_version = conf.producer_api_version
         assert api_version is not None
-        super().__init__(**kwargs)
+        super().__init__(loop=loop, **kwargs)
         self.buffer = ProducerBuffer(loop=self.loop, beacon=self.beacon)
         if conf.producer_threaded:
             self.threaded_producer = self.create_threaded_producer()