# BeamNG disconnecting during long executions

**URL:** <https://forum.beamng.tech/t/beamng-disconnecting-during-long-executions/427>\
**Category:** General\
**Tags:** map\
**Created:** [19 November 2025 15:23 UTC](https://forum.beamng.tech/t/beamng-disconnecting-during-long-executions/427 "2025-11-19T15:23:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![erick1-618](https://forum.beamng.tech/user_avatar/forum.beamng.tech/erick1-618/32/94_2.png) [@erick1-618](https://forum.beamng.tech/u/erick1-618)\
**Post date:** [19 November 2025 15:23 UTC](https://forum.beamng.tech/t/beamng-disconnecting-during-long-executions/427/1 "2025-11-19T15:23:33Z")

</div>

Hi everyone,

I’m running into a recurring issue when executing long BeamNGpy simulations. After initializing the scenario, I start the simulation and run a long data-collection loop where I update and read sensors. The problem is that during very long executions (for example, one million iterations or more), BeamNG eventually disconnects.

Here is the traceback I’m getting:

```auto
BNGDisconnectedError: The simulator ended the connection.

```

Sometimes I also get:

```auto
Connection reset by peer

```

This always happens only in long-running loops, never in short ones. My loop repeatedly calls `bng.step()` and reads the sensors each iteration. After enough iterations, the connection drops without any other warning.

Any help or suggestions would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![asaeed](https://forum.beamng.tech/user_avatar/forum.beamng.tech/asaeed/32/21_2.png) [@asaeed](https://forum.beamng.tech/u/asaeed)\
**Post date:** [20 November 2025 15:29 UTC](https://forum.beamng.tech/t/beamng-disconnecting-during-long-executions/427/2 "2025-11-20T15:29:10Z")

</div>

Hello @erick1-618,

Could you post the part of your code where you interact with BeamNGpy (e.g., the steps inside your loop)? It would help to see how you’re handling the connection and calls to `bng.step()`.

Thanks!

---

<div class="post-metadata">

**Author:** ![erick1-618](https://forum.beamng.tech/user_avatar/forum.beamng.tech/erick1-618/32/94_2.png) [@erick1-618](https://forum.beamng.tech/u/erick1-618)\
**Post date:** [23 November 2025 12:28 UTC](https://forum.beamng.tech/t/beamng-disconnecting-during-long-executions/427/3 "2025-11-23T12:28:46Z")

</div>

Sure, here it is:

I’ve removed some parts to make easier to understand. Basically just kepping the code that shows the flow of execution.

This is the loop function:

```auto
def simulation_loop(bng, sim_name):

    try:
        
        # Looping da simulação
        for i in range(1, length):

            # Avança 50 passos da simulação
            bng.step(15)

            vehicle.sensors.poll()

    except KeyboardInterrupt:
        print('\033[33m[WARN]\033[0m Interrompendo simulação')

```

And the main:

```auto
def main(report, sim_name):

    # Inicializa o BeamNG
    if not set_bng_container_up():
        return

    # Instanciando o BeamNG
    beamng = BeamNGpy(host="xxx.xxx.xxx.xxx", port=25252)
    beamng.open()

    # Configurando o cenário
    scenario = Scenario("west_coast_usa", "vehicle logging")
    vehicle = Vehicle("ego_vehicle", model="sbr", license="AAA000", part_config='vehicles/sbr/electric_300.pc')
    scenario.add_vehicle(
        vehicle,
        pos=(-769.1, 400.8, 142.8), rot_quat=(0.0173, -0.0019, -0.6354, 0.7720)
    )
    scenario.make(beamng)
    print("\033[34m[INFO]\033[0m Cenário construído")

    # Setando determinismo e iniciando cenário
    beamng.settings.set_deterministic(60)
    beamng.scenario.load(scenario)
    beamng.scenario.start()
    print("\033[34m[INFO]\033[0m Cenário iniciado")

    # Pausa a simulação, para ser controlada pelos steps
    beamng.pause()    

    # Instanciando APIs
    trafficApi = TrafficApi(beamng)

    # Instanciando e anexando sensor do veículo
    electrics = Electrics()
    vehicle.attach_sensor('electrics', electrics)
    powertrain = PowertrainSensor('powertrain', beamng, vehicle, is_send_immediately=True)

    # Configurando a câmera
    camera = Camera(
        "camera1",
        beamng,
        vehicle,
        requested_update_time=0.01,
        pos=(-0.3, 1, 2),
        dir=(0, -1, 0),
        field_of_view_y=70,
        near_far_planes=(0.1, 1000),
        resolution=(1024, 1024)
    )

    # Inserindo tráfego
    trafficApi.spawn(
        max_amount=2,
        police_ratio=0,
        extra_amount=2,
        parked_amount=10
    )  

    # Looping da simulação
    simulation_loop(beamng, nome_sim)

    vehicle.ai.set_mode("disabled")

    # Fecha a conexão com o BeamNg
    beamng.disconnect()

```

---

<div class="post-metadata">

**Author:** ![asaeed](https://forum.beamng.tech/user_avatar/forum.beamng.tech/asaeed/32/21_2.png) [@asaeed](https://forum.beamng.tech/u/asaeed)\
**Post date:** [26 November 2025 10:17 UTC](https://forum.beamng.tech/t/beamng-disconnecting-during-long-executions/427/4 "2025-11-26T10:17:42Z")

</div>

Hello @erick1-618

Can you provide us some rough estimation on your experiment duration; we have tried to run a very similar example for ~18 hours (that’s around 10000 times of calling `bng.step(50)`) and did not get any disconnect issue. Also, if BeamNG.tech crashed, please shae the crash report.

Best regards,  
Abdulrahman Saeed
