How do I fix "Unable to allocate buffer" error?

I had described my problem in my previous question.

In short, after a long time of a bunch of central BLE devices regularly connecting and disconnecting to my nRF52840 peripheral device  for data exchange the peripheral device fails to allocate buffers for subsequent connections and becomes unusable, the centrals try to connect but the peripheral won't let them anymore. I can't just ignore that.

The peripheral handles multiple connections at the same time.

How do I fix this? The only thing that works so far is rebooting the peripheral, but frequent and abrupt reboots are just not right. Is there a way to reset the buffers or BLE core or something to make the buffers allocate again? Or is there a way to prevent this from happening? Central devices have to disconnect and reconnect every time.

Parents
  • Hello,

    Please include the debug log here if you can. The memory should have been freed after the connection was terminated, so I am not sure what is happening here.

    Thanks,

    Vidar

  • I don't think I can provide internal debug logs for connections, when I add CONFIG_BT_DEBUG_CONN to prj.conf the amount of logs seem to cause the system to fault within seconds.

    This is to what extent I can share what's going on: connections, amount of data received on write char, amount of data written to notify char, disconnections. There are 5 central devices total.

    The error occurs when a central device connects and sends some data and the peripheral tries to reply.

    [00:12:15.371,246] <inf> handle_my_custom_service: Disconnected from My Device 3: reason 19)
    [00:12:15.927,978] <inf> handle_my_custom_service: Connected E0:6F:08:F4:03:C7 (random)
    [00:12:15.928,009] <inf> handle_my_custom_service: My Device 1
    [00:12:16.241,180] <inf> handle_my_custom_service: Connected CD:A4:C3:85:36:9B (random)
    [00:12:16.241,180] <inf> handle_my_custom_service: My Device 4
    [00:12:16.289,337] <inf> handle_my_custom_service: Received data from My Device 1 of len 4
    [00:12:16.289,337] <inf> handle_my_custom_service: reply to My Device 1 with data of len 2
    [00:12:16.289,428] <inf> my_custom_service_server: bt_gatt_notify_cb 0
    [00:12:16.357,788] <inf> handle_my_custom_service: Connected D0:C7:72:D6:75:B8 (random)
    [00:12:16.357,818] <inf> handle_my_custom_service: My Device 2
    [00:12:16.378,631] <inf> handle_my_custom_service: Received data from My Device 1 of len 12
    [00:12:16.378,662] <inf> handle_my_custom_service: reply to My Device 1 with data of len 2
    [00:12:16.378,753] <inf> my_custom_service_server: bt_gatt_notify_cb 0
    [00:12:16.438,720] <inf> handle_my_custom_service: Received data from My Device 1 of len 7
    [00:12:16.499,237] <inf> handle_my_custom_service: Disconnected from My Device 1: reason 19)
    [00:12:16.872,619] <inf> handle_my_custom_service: Received data from My Device 4 of len 4
    [00:12:16.872,650] <inf> handle_my_custom_service: reply to My Device 4 with data of len 2
    [00:12:16.872,711] <inf> my_custom_service_server: bt_gatt_notify_cb 0
    [00:12:17.022,003] <inf> handle_my_custom_service: Received data from My Device 4 of len 12
    [00:12:17.022,003] <inf> handle_my_custom_service: reply to My Device 4 with data of len 2
    [00:12:17.022,094] <inf> my_custom_service_server: bt_gatt_notify_cb 0
    [00:12:17.109,069] <inf> handle_my_custom_service: Received data from My Device 2 of len 4
    [00:12:17.109,069] <inf> handle_my_custom_service: reply to My Device 2 with data of len 2
    [00:12:17.109,161] <inf> my_custom_service_server: bt_gatt_notify_cb 0
    [00:12:17.141,998] <inf> handle_my_custom_service: Received data from My Device 4 of len 7
    [00:12:17.198,425] <inf> handle_my_custom_service: Received data from My Device 2 of len 12
    [00:12:17.198,455] <inf> handle_my_custom_service: reply to My Device 2 with data of len 2
    [00:12:17.198,547] <inf> my_custom_service_server: bt_gatt_notify_cb 0
    [00:12:17.232,543] <inf> handle_my_custom_service: Disconnected from My Device 4: reason 19)
    [00:12:17.288,360] <inf> handle_my_custom_service: Received data from My Device 2 of len 7
    [00:12:17.348,937] <inf> handle_my_custom_service: Disconnected from My Device 2: reason 19)
    [00:12:17.425,872] <inf> handle_my_custom_service: Connected CB:85:7E:12:2B:AB (random)
    [00:12:17.425,872] <inf> handle_my_custom_service: My Device 3
    [00:12:17.847,229] <inf> handle_my_custom_service: Received data from My Device 3 of len 4
    [00:12:17.847,229] <inf> handle_my_custom_service: reply to My Device 3 with data of len 2
    [00:12:17.847,351] <inf> my_custom_service_server: bt_gatt_notify_cb 0
    [00:12:17.906,707] <inf> handle_my_custom_service: Received data from My Device 3 of len 12
    [00:12:17.906,738] <inf> handle_my_custom_service: reply to My Device 3 with data of len 2
    [00:12:17.906,829] <inf> my_custom_service_server: bt_gatt_notify_cb 0
    [00:12:17.996,551] <inf> handle_my_custom_service: Received data from My Device 3 of len 7
    [00:12:18.056,793] <inf> handle_my_custom_service: Disconnected from My Device 3: reason 19)
    [00:12:18.372,070] <inf> handle_my_custom_service: Connected CD:A4:C3:85:36:9B (random)
    [00:12:18.372,070] <inf> handle_my_custom_service: My Device 4
    [00:12:18.584,411] <inf> handle_my_custom_service: Connected D0:C7:72:D6:75:B8 (random)
    [00:12:18.584,442] <inf> handle_my_custom_service: My Device 2
    [00:12:18.700,988] <inf> handle_my_custom_service: Connected CB:85:7E:12:2B:AB (random)
    [00:12:18.701,019] <inf> handle_my_custom_service: My Device 3
    [00:12:18.816,833] <inf> handle_my_custom_service: Connected E0:6F:08:F4:03:C7 (random)
    [00:12:18.816,864] <inf> handle_my_custom_service: My Device 1
    [00:12:19.275,390] <inf> handle_my_custom_service: Received data from My Device 4 of len 4
    [00:12:19.275,390] <inf> handle_my_custom_service: reply to My Device 4 with data of len 2
    [00:12:19.333,557] <wrn> bt_conn: Disconnected while allocating context
    [00:12:19.333,557] <inf> my_custom_service_server: bt_gatt_notify_cb -128
    [00:12:25.372,039] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:12:25.372,070] <err> bt_l2cap: Unable to allocate buffer for op 0x12
    [00:12:27.372,161] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:12:27.372,192] <err> bt_l2cap: Unable to allocate buffer for op 0x12
    [00:12:29.372,283] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:12:29.372,314] <err> bt_l2cap: Unable to allocate buffer for op 0x12
    [00:12:31.372,406] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:12:31.372,436] <err> bt_l2cap: Unable to allocate buffer for op 0x12
    [00:12:49.333,709] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:12:49.333,740] <err> bt_att: Unable to allocate buffer for op 0x05
    [00:13:19.333,801] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:13:19.333,831] <err> bt_att: Unable to allocate buffer for op 0x01
    [00:13:19.333,984] <wrn> bt_att: Not connected
    [00:13:19.334,136] <inf> handle_my_custom_service: Disconnected from My Device 4: reason 19)
    [00:13:49.334,075] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:13:49.334,106] <err> bt_att: Unable to allocate buffer for op 0x01
    [00:14:19.334,228] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:14:19.334,259] <err> bt_att: Unable to allocate buffer for op 0x01
    [00:14:19.335,205] <inf> handle_my_custom_service: Connected D0:C7:72:D6:75:B8 (random)
    [00:14:19.335,235] <inf> handle_my_custom_service: My Device 2
    [00:14:19.335,388] <wrn> bt_att: Not connected
    [00:14:19.335,540] <inf> handle_my_custom_service: Disconnected from My Device 2: reason 19)
    [00:14:26.335,174] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:14:26.335,205] <err> bt_l2cap: Unable to allocate buffer for op 0x12
    [00:14:49.335,479] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:14:49.335,510] <err> bt_att: Unable to allocate buffer for op 0x01
    [00:14:49.336,669] <inf> handle_my_custom_service: Connected FE:18:21:43:13:05 (random)
    [00:14:49.336,669] <wrn> handle_my_custom_service: New Device
    [00:14:49.337,310] <inf> handle_my_custom_service: Disconnected from My Device 1: reason 19)
    [00:14:49.337,432] <inf> handle_my_custom_service: Disconnected from My Device 3: reason 19)
    [00:14:49.337,554] <inf> handle_my_custom_service: Disconnected from My Device 2: reason 19)
    [00:14:56.336,639] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:14:56.336,669] <err> bt_l2cap: Unable to allocate buffer for op 0x12
    [00:15:19.337,249] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:15:19.337,280] <err> bt_att: Unable to allocate buffer for op 0x07
    [00:15:49.337,341] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:15:49.337,371] <err> bt_att: Unable to allocate buffer for op 0x01
    [00:15:49.338,317] <inf> handle_my_custom_service: Connected CB:85:7E:12:2B:AB (random)
    [00:15:49.338,317] <inf> handle_my_custom_service: My Device 3
    [00:15:49.338,470] <wrn> bt_att: Not connected
    [00:15:49.338,775] <inf> handle_my_custom_service: Disconnected from New Device FE:18:21:43:13:05 (random): reason 19)
    [00:15:56.338,287] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:15:56.338,317] <err> bt_l2cap: Unable to allocate buffer for op 0x12
    

    Everything above "my_custom_service_server: bt_gatt_notify_cb -128" is how it's supposed to work, and it does for about 15 minutes. Once bt_gatt_notify_cb returns -ENOTCONN (-128) the connections can't be established anymore. I check for connection twice before calling bt_gatt_notify_cb.

        if (!conn) {
            return -ENOTCONN;
        } else if (bt_gatt_is_subscribed(conn, attr, BT_GATT_CCC_NOTIFY)) {
            int err = bt_gatt_notify_cb(conn, &params);
            LOG_INF("bt_gatt_notify_cb %d", err);
            return err;
        }

    I can get a few of occasional "bt_conn: Disconnected while allocating context" or "bt_att: Not connected" before the connection failures start, but subsequent connections are OK, so it doesn't always result in this error.

Reply
  • I don't think I can provide internal debug logs for connections, when I add CONFIG_BT_DEBUG_CONN to prj.conf the amount of logs seem to cause the system to fault within seconds.

    This is to what extent I can share what's going on: connections, amount of data received on write char, amount of data written to notify char, disconnections. There are 5 central devices total.

    The error occurs when a central device connects and sends some data and the peripheral tries to reply.

    [00:12:15.371,246] <inf> handle_my_custom_service: Disconnected from My Device 3: reason 19)
    [00:12:15.927,978] <inf> handle_my_custom_service: Connected E0:6F:08:F4:03:C7 (random)
    [00:12:15.928,009] <inf> handle_my_custom_service: My Device 1
    [00:12:16.241,180] <inf> handle_my_custom_service: Connected CD:A4:C3:85:36:9B (random)
    [00:12:16.241,180] <inf> handle_my_custom_service: My Device 4
    [00:12:16.289,337] <inf> handle_my_custom_service: Received data from My Device 1 of len 4
    [00:12:16.289,337] <inf> handle_my_custom_service: reply to My Device 1 with data of len 2
    [00:12:16.289,428] <inf> my_custom_service_server: bt_gatt_notify_cb 0
    [00:12:16.357,788] <inf> handle_my_custom_service: Connected D0:C7:72:D6:75:B8 (random)
    [00:12:16.357,818] <inf> handle_my_custom_service: My Device 2
    [00:12:16.378,631] <inf> handle_my_custom_service: Received data from My Device 1 of len 12
    [00:12:16.378,662] <inf> handle_my_custom_service: reply to My Device 1 with data of len 2
    [00:12:16.378,753] <inf> my_custom_service_server: bt_gatt_notify_cb 0
    [00:12:16.438,720] <inf> handle_my_custom_service: Received data from My Device 1 of len 7
    [00:12:16.499,237] <inf> handle_my_custom_service: Disconnected from My Device 1: reason 19)
    [00:12:16.872,619] <inf> handle_my_custom_service: Received data from My Device 4 of len 4
    [00:12:16.872,650] <inf> handle_my_custom_service: reply to My Device 4 with data of len 2
    [00:12:16.872,711] <inf> my_custom_service_server: bt_gatt_notify_cb 0
    [00:12:17.022,003] <inf> handle_my_custom_service: Received data from My Device 4 of len 12
    [00:12:17.022,003] <inf> handle_my_custom_service: reply to My Device 4 with data of len 2
    [00:12:17.022,094] <inf> my_custom_service_server: bt_gatt_notify_cb 0
    [00:12:17.109,069] <inf> handle_my_custom_service: Received data from My Device 2 of len 4
    [00:12:17.109,069] <inf> handle_my_custom_service: reply to My Device 2 with data of len 2
    [00:12:17.109,161] <inf> my_custom_service_server: bt_gatt_notify_cb 0
    [00:12:17.141,998] <inf> handle_my_custom_service: Received data from My Device 4 of len 7
    [00:12:17.198,425] <inf> handle_my_custom_service: Received data from My Device 2 of len 12
    [00:12:17.198,455] <inf> handle_my_custom_service: reply to My Device 2 with data of len 2
    [00:12:17.198,547] <inf> my_custom_service_server: bt_gatt_notify_cb 0
    [00:12:17.232,543] <inf> handle_my_custom_service: Disconnected from My Device 4: reason 19)
    [00:12:17.288,360] <inf> handle_my_custom_service: Received data from My Device 2 of len 7
    [00:12:17.348,937] <inf> handle_my_custom_service: Disconnected from My Device 2: reason 19)
    [00:12:17.425,872] <inf> handle_my_custom_service: Connected CB:85:7E:12:2B:AB (random)
    [00:12:17.425,872] <inf> handle_my_custom_service: My Device 3
    [00:12:17.847,229] <inf> handle_my_custom_service: Received data from My Device 3 of len 4
    [00:12:17.847,229] <inf> handle_my_custom_service: reply to My Device 3 with data of len 2
    [00:12:17.847,351] <inf> my_custom_service_server: bt_gatt_notify_cb 0
    [00:12:17.906,707] <inf> handle_my_custom_service: Received data from My Device 3 of len 12
    [00:12:17.906,738] <inf> handle_my_custom_service: reply to My Device 3 with data of len 2
    [00:12:17.906,829] <inf> my_custom_service_server: bt_gatt_notify_cb 0
    [00:12:17.996,551] <inf> handle_my_custom_service: Received data from My Device 3 of len 7
    [00:12:18.056,793] <inf> handle_my_custom_service: Disconnected from My Device 3: reason 19)
    [00:12:18.372,070] <inf> handle_my_custom_service: Connected CD:A4:C3:85:36:9B (random)
    [00:12:18.372,070] <inf> handle_my_custom_service: My Device 4
    [00:12:18.584,411] <inf> handle_my_custom_service: Connected D0:C7:72:D6:75:B8 (random)
    [00:12:18.584,442] <inf> handle_my_custom_service: My Device 2
    [00:12:18.700,988] <inf> handle_my_custom_service: Connected CB:85:7E:12:2B:AB (random)
    [00:12:18.701,019] <inf> handle_my_custom_service: My Device 3
    [00:12:18.816,833] <inf> handle_my_custom_service: Connected E0:6F:08:F4:03:C7 (random)
    [00:12:18.816,864] <inf> handle_my_custom_service: My Device 1
    [00:12:19.275,390] <inf> handle_my_custom_service: Received data from My Device 4 of len 4
    [00:12:19.275,390] <inf> handle_my_custom_service: reply to My Device 4 with data of len 2
    [00:12:19.333,557] <wrn> bt_conn: Disconnected while allocating context
    [00:12:19.333,557] <inf> my_custom_service_server: bt_gatt_notify_cb -128
    [00:12:25.372,039] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:12:25.372,070] <err> bt_l2cap: Unable to allocate buffer for op 0x12
    [00:12:27.372,161] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:12:27.372,192] <err> bt_l2cap: Unable to allocate buffer for op 0x12
    [00:12:29.372,283] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:12:29.372,314] <err> bt_l2cap: Unable to allocate buffer for op 0x12
    [00:12:31.372,406] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:12:31.372,436] <err> bt_l2cap: Unable to allocate buffer for op 0x12
    [00:12:49.333,709] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:12:49.333,740] <err> bt_att: Unable to allocate buffer for op 0x05
    [00:13:19.333,801] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:13:19.333,831] <err> bt_att: Unable to allocate buffer for op 0x01
    [00:13:19.333,984] <wrn> bt_att: Not connected
    [00:13:19.334,136] <inf> handle_my_custom_service: Disconnected from My Device 4: reason 19)
    [00:13:49.334,075] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:13:49.334,106] <err> bt_att: Unable to allocate buffer for op 0x01
    [00:14:19.334,228] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:14:19.334,259] <err> bt_att: Unable to allocate buffer for op 0x01
    [00:14:19.335,205] <inf> handle_my_custom_service: Connected D0:C7:72:D6:75:B8 (random)
    [00:14:19.335,235] <inf> handle_my_custom_service: My Device 2
    [00:14:19.335,388] <wrn> bt_att: Not connected
    [00:14:19.335,540] <inf> handle_my_custom_service: Disconnected from My Device 2: reason 19)
    [00:14:26.335,174] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:14:26.335,205] <err> bt_l2cap: Unable to allocate buffer for op 0x12
    [00:14:49.335,479] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:14:49.335,510] <err> bt_att: Unable to allocate buffer for op 0x01
    [00:14:49.336,669] <inf> handle_my_custom_service: Connected FE:18:21:43:13:05 (random)
    [00:14:49.336,669] <wrn> handle_my_custom_service: New Device
    [00:14:49.337,310] <inf> handle_my_custom_service: Disconnected from My Device 1: reason 19)
    [00:14:49.337,432] <inf> handle_my_custom_service: Disconnected from My Device 3: reason 19)
    [00:14:49.337,554] <inf> handle_my_custom_service: Disconnected from My Device 2: reason 19)
    [00:14:56.336,639] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:14:56.336,669] <err> bt_l2cap: Unable to allocate buffer for op 0x12
    [00:15:19.337,249] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:15:19.337,280] <err> bt_att: Unable to allocate buffer for op 0x07
    [00:15:49.337,341] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:15:49.337,371] <err> bt_att: Unable to allocate buffer for op 0x01
    [00:15:49.338,317] <inf> handle_my_custom_service: Connected CB:85:7E:12:2B:AB (random)
    [00:15:49.338,317] <inf> handle_my_custom_service: My Device 3
    [00:15:49.338,470] <wrn> bt_att: Not connected
    [00:15:49.338,775] <inf> handle_my_custom_service: Disconnected from New Device FE:18:21:43:13:05 (random): reason 19)
    [00:15:56.338,287] <wrn> bt_conn: Unable to allocate buffer within timeout
    [00:15:56.338,317] <err> bt_l2cap: Unable to allocate buffer for op 0x12
    

    Everything above "my_custom_service_server: bt_gatt_notify_cb -128" is how it's supposed to work, and it does for about 15 minutes. Once bt_gatt_notify_cb returns -ENOTCONN (-128) the connections can't be established anymore. I check for connection twice before calling bt_gatt_notify_cb.

        if (!conn) {
            return -ENOTCONN;
        } else if (bt_gatt_is_subscribed(conn, attr, BT_GATT_CCC_NOTIFY)) {
            int err = bt_gatt_notify_cb(conn, &params);
            LOG_INF("bt_gatt_notify_cb %d", err);
            return err;
        }

    I can get a few of occasional "bt_conn: Disconnected while allocating context" or "bt_att: Not connected" before the connection failures start, but subsequent connections are OK, so it doesn't always result in this error.

Children
No Data
Related