ClickHouse / clickhouse-rs

Official pure Rust typed client for ClickHouse DB
https://clickhouse.com
Apache License 2.0
322 stars 92 forks source link

after few hundred inserts to db, the library panics #137

Closed nuel77 closed 2 months ago

nuel77 commented 2 months ago

using version : 0.12.2

Hello, After a few hundred consecutive writes, the client panics for no reason. maybe i am using this wrong. any help is appreciated. i am writing the the database using this function:

 pub async fn store_data<T>(&self, items: &Vec<T>, table: &str) -> anyhow::Result<()>
    where
        T: Row + Serialize,
    {
        let mut inserter = clickhouse.insert(table).unwrap();
        for item in items.iter() {
            inserter.write(item).await.unwrap();
        }
        inserter
            .end()
            .await
            .context("cannot commit inserter for table {:table?}")?;
        Ok(())
    }

after some writes, the db panics with this error message:

image

this assertion from the crate is failing:

    fn terminated(&mut self) {
        debug_assert!(matches!(self, InsertState::Active { .. }));
        replace_with_or_abort(self, |_self| match _self {
            InsertState::Active { handle, .. } => InsertState::Terminated { handle },
            _ => unreachable!(),
        });
    }
slvrtrn commented 2 months ago

This, indeed, is not supposed to happen.

Can you please provide a runnable minimal repro? I tried to write a quick test with many parallel inserts (I used some test helper functions from here):

#[tokio::test]
async fn does_not_panic_on_consequent_calls() {
    let table_name = "does_not_panic_on_consequent_calls";
    let client: Client = prepare_database!();
    create_simple_table(&client, table_name).await;

    const ITERATIONS: usize = 100;
    const ROWS_PER_ITERATION: usize = 10_000;

    let mut set = JoinSet::new();
    for i in 0..ITERATIONS {
        let client = client.clone();
        set.spawn(async move {
            insert_rows(&client, table_name, ROWS_PER_ITERATION, i).await;
        });
    }
    while let Some(_) = set.join_next().await {
        // ...
    }

    let rows = fetch_simple_rows(&client, table_name).await;
    assert_eq!(rows.len(), ITERATIONS * ROWS_PER_ITERATION)
}

async fn insert_rows(client: &Client, table_name: &str, count: usize, iteration: usize) {
    println!(
        "[{}] Started inserting rows - iter {}",
        OffsetDateTime::now_utc().to_string(),
        iteration
    );
    let mut insert = client.insert(table_name).unwrap();
    for _ in 0..count {
        let row = random_row();
        insert.write(&row).await.unwrap();
    }
    insert.end().await.unwrap();
    println!(
        "[{}] Inserted {} rows - iter {}",
        OffsetDateTime::now_utc().to_string(),
        count,
        iteration
    );
}

fn random_row() -> SimpleRow {
    let mut rng = thread_rng();
    SimpleRow::new(rng.gen(), rng.gen::<u32>().to_string())
}

But there are no panics, no matter how many iterations (or, more precisely, task count) or the number of rows per iteration are used.

Sample output:

``` [2024-09-03 14:17:35.760235 +00:00:00] Started inserting rows - iter 0 [2024-09-03 14:17:35.779601 +00:00:00] Started inserting rows - iter 1 [2024-09-03 14:17:35.798666 +00:00:00] Started inserting rows - iter 2 [2024-09-03 14:17:35.817964 +00:00:00] Started inserting rows - iter 3 [2024-09-03 14:17:35.837937 +00:00:00] Started inserting rows - iter 4 [2024-09-03 14:17:35.85887 +00:00:00] Started inserting rows - iter 5 [2024-09-03 14:17:35.880984 +00:00:00] Started inserting rows - iter 6 [2024-09-03 14:17:35.899194 +00:00:00] Started inserting rows - iter 7 [2024-09-03 14:17:35.916096 +00:00:00] Started inserting rows - iter 8 [2024-09-03 14:17:35.932904 +00:00:00] Started inserting rows - iter 9 [2024-09-03 14:17:35.949307 +00:00:00] Started inserting rows - iter 10 [2024-09-03 14:17:35.965143 +00:00:00] Started inserting rows - iter 11 [2024-09-03 14:17:35.980456 +00:00:00] Started inserting rows - iter 12 [2024-09-03 14:17:35.995942 +00:00:00] Started inserting rows - iter 13 [2024-09-03 14:17:36.011558 +00:00:00] Started inserting rows - iter 14 [2024-09-03 14:17:36.027282 +00:00:00] Started inserting rows - iter 15 [2024-09-03 14:17:36.042697 +00:00:00] Started inserting rows - iter 16 [2024-09-03 14:17:36.058154 +00:00:00] Started inserting rows - iter 17 [2024-09-03 14:17:36.073581 +00:00:00] Started inserting rows - iter 18 [2024-09-03 14:17:36.087699 +00:00:00] Started inserting rows - iter 19 [2024-09-03 14:17:36.102976 +00:00:00] Started inserting rows - iter 20 [2024-09-03 14:17:36.11875 +00:00:00] Started inserting rows - iter 21 [2024-09-03 14:17:36.134139 +00:00:00] Started inserting rows - iter 22 [2024-09-03 14:17:36.149327 +00:00:00] Started inserting rows - iter 23 [2024-09-03 14:17:36.164857 +00:00:00] Started inserting rows - iter 24 [2024-09-03 14:17:36.18104 +00:00:00] Started inserting rows - iter 25 [2024-09-03 14:17:36.196381 +00:00:00] Started inserting rows - iter 26 [2024-09-03 14:17:36.211629 +00:00:00] Started inserting rows - iter 27 [2024-09-03 14:17:36.226307 +00:00:00] Started inserting rows - iter 28 [2024-09-03 14:17:36.242002 +00:00:00] Started inserting rows - iter 29 [2024-09-03 14:17:36.257026 +00:00:00] Started inserting rows - iter 30 [2024-09-03 14:17:36.272396 +00:00:00] Started inserting rows - iter 31 [2024-09-03 14:17:36.287957 +00:00:00] Started inserting rows - iter 32 [2024-09-03 14:17:36.303203 +00:00:00] Started inserting rows - iter 33 [2024-09-03 14:17:36.318248 +00:00:00] Started inserting rows - iter 34 [2024-09-03 14:17:36.333645 +00:00:00] Started inserting rows - iter 35 [2024-09-03 14:17:36.349612 +00:00:00] Started inserting rows - iter 36 [2024-09-03 14:17:36.365142 +00:00:00] Started inserting rows - iter 37 [2024-09-03 14:17:36.380335 +00:00:00] Started inserting rows - iter 38 [2024-09-03 14:17:36.395019 +00:00:00] Started inserting rows - iter 39 [2024-09-03 14:17:36.40919 +00:00:00] Started inserting rows - iter 40 [2024-09-03 14:17:36.424072 +00:00:00] Started inserting rows - iter 41 [2024-09-03 14:17:36.439149 +00:00:00] Started inserting rows - iter 42 [2024-09-03 14:17:36.454136 +00:00:00] Started inserting rows - iter 43 [2024-09-03 14:17:36.46869 +00:00:00] Started inserting rows - iter 44 [2024-09-03 14:17:36.483772 +00:00:00] Started inserting rows - iter 45 [2024-09-03 14:17:36.49902 +00:00:00] Started inserting rows - iter 46 [2024-09-03 14:17:36.5144 +00:00:00] Started inserting rows - iter 47 [2024-09-03 14:17:36.529636 +00:00:00] Started inserting rows - iter 48 [2024-09-03 14:17:36.544822 +00:00:00] Started inserting rows - iter 49 [2024-09-03 14:17:36.560144 +00:00:00] Started inserting rows - iter 50 [2024-09-03 14:17:36.57479 +00:00:00] Started inserting rows - iter 51 [2024-09-03 14:17:36.589252 +00:00:00] Started inserting rows - iter 52 [2024-09-03 14:17:36.604131 +00:00:00] Started inserting rows - iter 53 [2024-09-03 14:17:36.618968 +00:00:00] Started inserting rows - iter 54 [2024-09-03 14:17:36.633826 +00:00:00] Started inserting rows - iter 55 [2024-09-03 14:17:36.648768 +00:00:00] Started inserting rows - iter 56 [2024-09-03 14:17:36.663358 +00:00:00] Started inserting rows - iter 57 [2024-09-03 14:17:36.677352 +00:00:00] Started inserting rows - iter 58 [2024-09-03 14:17:36.691664 +00:00:00] Started inserting rows - iter 59 [2024-09-03 14:17:36.70651 +00:00:00] Started inserting rows - iter 60 [2024-09-03 14:17:36.721149 +00:00:00] Started inserting rows - iter 61 [2024-09-03 14:17:36.736083 +00:00:00] Started inserting rows - iter 62 [2024-09-03 14:17:36.750967 +00:00:00] Started inserting rows - iter 63 [2024-09-03 14:17:36.76641 +00:00:00] Started inserting rows - iter 64 [2024-09-03 14:17:36.780802 +00:00:00] Started inserting rows - iter 65 [2024-09-03 14:17:36.795595 +00:00:00] Started inserting rows - iter 66 [2024-09-03 14:17:36.81045 +00:00:00] Started inserting rows - iter 67 [2024-09-03 14:17:36.825026 +00:00:00] Started inserting rows - iter 68 [2024-09-03 14:17:36.839687 +00:00:00] Started inserting rows - iter 69 [2024-09-03 14:17:36.854595 +00:00:00] Started inserting rows - iter 70 [2024-09-03 14:17:36.869435 +00:00:00] Started inserting rows - iter 71 [2024-09-03 14:17:36.883945 +00:00:00] Started inserting rows - iter 72 [2024-09-03 14:17:36.898921 +00:00:00] Started inserting rows - iter 73 [2024-09-03 14:17:36.913725 +00:00:00] Started inserting rows - iter 74 [2024-09-03 14:17:36.928692 +00:00:00] Started inserting rows - iter 75 [2024-09-03 14:17:36.942818 +00:00:00] Started inserting rows - iter 76 [2024-09-03 14:17:36.957603 +00:00:00] Started inserting rows - iter 77 [2024-09-03 14:17:36.972391 +00:00:00] Started inserting rows - iter 78 [2024-09-03 14:17:36.987478 +00:00:00] Started inserting rows - iter 79 [2024-09-03 14:17:37.002136 +00:00:00] Started inserting rows - iter 80 [2024-09-03 14:17:37.016918 +00:00:00] Started inserting rows - iter 81 [2024-09-03 14:17:37.031568 +00:00:00] Started inserting rows - iter 82 [2024-09-03 14:17:37.046106 +00:00:00] Started inserting rows - iter 83 [2024-09-03 14:17:37.060869 +00:00:00] Started inserting rows - iter 84 [2024-09-03 14:17:37.07518 +00:00:00] Started inserting rows - iter 85 [2024-09-03 14:17:37.089806 +00:00:00] Started inserting rows - iter 86 [2024-09-03 14:17:37.105043 +00:00:00] Started inserting rows - iter 87 [2024-09-03 14:17:37.119699 +00:00:00] Started inserting rows - iter 88 [2024-09-03 14:17:37.134116 +00:00:00] Started inserting rows - iter 89 [2024-09-03 14:17:37.148464 +00:00:00] Started inserting rows - iter 90 [2024-09-03 14:17:37.162353 +00:00:00] Started inserting rows - iter 91 [2024-09-03 14:17:37.177204 +00:00:00] Started inserting rows - iter 92 [2024-09-03 14:17:37.192167 +00:00:00] Started inserting rows - iter 93 [2024-09-03 14:17:37.207107 +00:00:00] Started inserting rows - iter 94 [2024-09-03 14:17:37.222243 +00:00:00] Started inserting rows - iter 95 [2024-09-03 14:17:37.237095 +00:00:00] Started inserting rows - iter 96 [2024-09-03 14:17:37.252113 +00:00:00] Started inserting rows - iter 97 [2024-09-03 14:17:37.266618 +00:00:00] Started inserting rows - iter 98 [2024-09-03 14:17:37.281801 +00:00:00] Started inserting rows - iter 99 [2024-09-03 14:17:37.316188 +00:00:00] Inserted 10000 rows - iter 0 [2024-09-03 14:17:37.318794 +00:00:00] Inserted 10000 rows - iter 2 [2024-09-03 14:17:37.318886 +00:00:00] Inserted 10000 rows - iter 35 [2024-09-03 14:17:37.319043 +00:00:00] Inserted 10000 rows - iter 27 [2024-09-03 14:17:37.321628 +00:00:00] Inserted 10000 rows - iter 51 [2024-09-03 14:17:37.322597 +00:00:00] Inserted 10000 rows - iter 1 [2024-09-03 14:17:37.324024 +00:00:00] Inserted 10000 rows - iter 3 [2024-09-03 14:17:37.32541 +00:00:00] Inserted 10000 rows - iter 73 [2024-09-03 14:17:37.325506 +00:00:00] Inserted 10000 rows - iter 79 [2024-09-03 14:17:37.327355 +00:00:00] Inserted 10000 rows - iter 5 [2024-09-03 14:17:37.327439 +00:00:00] Inserted 10000 rows - iter 4 [2024-09-03 14:17:37.327492 +00:00:00] Inserted 10000 rows - iter 7 [2024-09-03 14:17:37.329821 +00:00:00] Inserted 10000 rows - iter 9 [2024-09-03 14:17:37.332959 +00:00:00] Inserted 10000 rows - iter 6 [2024-09-03 14:17:37.333127 +00:00:00] Inserted 10000 rows - iter 13 [2024-09-03 14:17:37.333738 +00:00:00] Inserted 10000 rows - iter 10 [2024-09-03 14:17:37.337263 +00:00:00] Inserted 10000 rows - iter 15 [2024-09-03 14:17:37.337357 +00:00:00] Inserted 10000 rows - iter 84 [2024-09-03 14:17:37.337432 +00:00:00] Inserted 10000 rows - iter 16 [2024-09-03 14:17:37.338285 +00:00:00] Inserted 10000 rows - iter 14 [2024-09-03 14:17:37.338609 +00:00:00] Inserted 10000 rows - iter 21 [2024-09-03 14:17:37.34209 +00:00:00] Inserted 10000 rows - iter 22 [2024-09-03 14:17:37.34304 +00:00:00] Inserted 10000 rows - iter 25 [2024-09-03 14:17:37.343887 +00:00:00] Inserted 10000 rows - iter 28 [2024-09-03 14:17:37.346005 +00:00:00] Inserted 10000 rows - iter 98 [2024-09-03 14:17:37.350172 +00:00:00] Inserted 10000 rows - iter 31 [2024-09-03 14:17:37.351089 +00:00:00] Inserted 10000 rows - iter 29 [2024-09-03 14:17:37.351395 +00:00:00] Inserted 10000 rows - iter 32 [2024-09-03 14:17:37.351464 +00:00:00] Inserted 10000 rows - iter 33 [2024-09-03 14:17:37.351963 +00:00:00] Inserted 10000 rows - iter 34 [2024-09-03 14:17:37.35258 +00:00:00] Inserted 10000 rows - iter 12 [2024-09-03 14:17:37.353501 +00:00:00] Inserted 10000 rows - iter 36 [2024-09-03 14:17:37.355486 +00:00:00] Inserted 10000 rows - iter 37 [2024-09-03 14:17:37.355693 +00:00:00] Inserted 10000 rows - iter 99 [2024-09-03 14:17:37.35677 +00:00:00] Inserted 10000 rows - iter 39 [2024-09-03 14:17:37.359129 +00:00:00] Inserted 10000 rows - iter 40 [2024-09-03 14:17:37.359222 +00:00:00] Inserted 10000 rows - iter 92 [2024-09-03 14:17:37.359895 +00:00:00] Inserted 10000 rows - iter 11 [2024-09-03 14:17:37.360151 +00:00:00] Inserted 10000 rows - iter 41 [2024-09-03 14:17:37.363537 +00:00:00] Inserted 10000 rows - iter 42 [2024-09-03 14:17:37.365693 +00:00:00] Inserted 10000 rows - iter 45 [2024-09-03 14:17:37.365915 +00:00:00] Inserted 10000 rows - iter 8 [2024-09-03 14:17:37.368821 +00:00:00] Inserted 10000 rows - iter 52 [2024-09-03 14:17:37.372044 +00:00:00] Inserted 10000 rows - iter 43 [2024-09-03 14:17:37.37529 +00:00:00] Inserted 10000 rows - iter 55 [2024-09-03 14:17:37.3771 +00:00:00] Inserted 10000 rows - iter 80 [2024-09-03 14:17:37.379109 +00:00:00] Inserted 10000 rows - iter 82 [2024-09-03 14:17:37.381209 +00:00:00] Inserted 10000 rows - iter 50 [2024-09-03 14:17:37.386326 +00:00:00] Inserted 10000 rows - iter 38 [2024-09-03 14:17:37.388108 +00:00:00] Inserted 10000 rows - iter 17 [2024-09-03 14:17:37.395044 +00:00:00] Inserted 10000 rows - iter 30 [2024-09-03 14:17:37.395242 +00:00:00] Inserted 10000 rows - iter 60 [2024-09-03 14:17:37.395257 +00:00:00] Inserted 10000 rows - iter 90 [2024-09-03 14:17:37.401967 +00:00:00] Inserted 10000 rows - iter 81 [2024-09-03 14:17:37.402016 +00:00:00] Inserted 10000 rows - iter 64 [2024-09-03 14:17:37.402028 +00:00:00] Inserted 10000 rows - iter 66 [2024-09-03 14:17:37.402037 +00:00:00] Inserted 10000 rows - iter 74 [2024-09-03 14:17:37.402047 +00:00:00] Inserted 10000 rows - iter 96 [2024-09-03 14:17:37.403783 +00:00:00] Inserted 10000 rows - iter 76 [2024-09-03 14:17:37.403813 +00:00:00] Inserted 10000 rows - iter 72 [2024-09-03 14:17:37.403835 +00:00:00] Inserted 10000 rows - iter 59 [2024-09-03 14:17:37.403845 +00:00:00] Inserted 10000 rows - iter 97 [2024-09-03 14:17:37.404029 +00:00:00] Inserted 10000 rows - iter 94 [2024-09-03 14:17:37.404042 +00:00:00] Inserted 10000 rows - iter 65 [2024-09-03 14:17:37.404051 +00:00:00] Inserted 10000 rows - iter 70 [2024-09-03 14:17:37.404573 +00:00:00] Inserted 10000 rows - iter 71 [2024-09-03 14:17:37.40538 +00:00:00] Inserted 10000 rows - iter 68 [2024-09-03 14:17:37.406071 +00:00:00] Inserted 10000 rows - iter 67 [2024-09-03 14:17:37.407117 +00:00:00] Inserted 10000 rows - iter 83 [2024-09-03 14:17:37.407411 +00:00:00] Inserted 10000 rows - iter 88 [2024-09-03 14:17:37.407767 +00:00:00] Inserted 10000 rows - iter 69 [2024-09-03 14:17:37.408498 +00:00:00] Inserted 10000 rows - iter 62 [2024-09-03 14:17:37.411863 +00:00:00] Inserted 10000 rows - iter 85 [2024-09-03 14:17:37.411896 +00:00:00] Inserted 10000 rows - iter 53 [2024-09-03 14:17:37.411907 +00:00:00] Inserted 10000 rows - iter 91 [2024-09-03 14:17:37.411989 +00:00:00] Inserted 10000 rows - iter 24 [2024-09-03 14:17:37.41279 +00:00:00] Inserted 10000 rows - iter 19 [2024-09-03 14:17:37.412917 +00:00:00] Inserted 10000 rows - iter 18 [2024-09-03 14:17:37.417571 +00:00:00] Inserted 10000 rows - iter 20 [2024-09-03 14:17:37.417685 +00:00:00] Inserted 10000 rows - iter 75 [2024-09-03 14:17:37.418285 +00:00:00] Inserted 10000 rows - iter 23 [2024-09-03 14:17:37.418552 +00:00:00] Inserted 10000 rows - iter 26 [2024-09-03 14:17:37.419263 +00:00:00] Inserted 10000 rows - iter 63 [2024-09-03 14:17:37.568822 +00:00:00] Inserted 10000 rows - iter 61 [2024-09-03 14:17:37.571072 +00:00:00] Inserted 10000 rows - iter 46 [2024-09-03 14:17:37.571839 +00:00:00] Inserted 10000 rows - iter 86 [2024-09-03 14:17:37.572345 +00:00:00] Inserted 10000 rows - iter 44 [2024-09-03 14:17:37.572468 +00:00:00] Inserted 10000 rows - iter 49 [2024-09-03 14:17:37.578452 +00:00:00] Inserted 10000 rows - iter 47 [2024-09-03 14:17:37.580492 +00:00:00] Inserted 10000 rows - iter 56 [2024-09-03 14:17:37.5806 +00:00:00] Inserted 10000 rows - iter 57 [2024-09-03 14:17:37.580879 +00:00:00] Inserted 10000 rows - iter 87 [2024-09-03 14:17:37.581806 +00:00:00] Inserted 10000 rows - iter 48 [2024-09-03 14:17:37.618373 +00:00:00] Inserted 10000 rows - iter 54 [2024-09-03 14:17:37.618452 +00:00:00] Inserted 10000 rows - iter 58 [2024-09-03 14:17:37.634379 +00:00:00] Inserted 10000 rows - iter 89 [2024-09-03 14:17:38.375221 +00:00:00] Inserted 10000 rows - iter 77 [2024-09-03 14:17:38.375665 +00:00:00] Inserted 10000 rows - iter 78 [2024-09-03 14:17:38.378197 +00:00:00] Inserted 10000 rows - iter 95 [2024-09-03 14:17:38.378566 +00:00:00] Inserted 10000 rows - iter 93 ```
vazw commented 2 months ago

Hey I was getting the same issues not sure why But when I revert my commit that isn't part of clickhouse it just work.

thread 'tokio-runtime-worker' panicked at /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/clickhouse-0.12.2/src/insert.rs:90:18:
internal error: entered unreachable code

in my case I was using aya_ebpf to monitor my traffic and these panic cause show up on these change and it's just never panic after revert from this

diff --git a/analytics-ebpf/src/utils.rs b/analytics-ebpf/src/utils.rs
index 4bb16bb..3a77121 100644
--- a/analytics-ebpf/src/utils.rs
+++ b/analytics-ebpf/src/utils.rs
@@ -26,15 +26,54 @@ pub fn ptr_at<T>(ctx: &XdpContext, offset: usize) -> Result<&T, u32> {
 pub fn is_our_ip(ip_addr: Ipv4Addr) -> bool {
     matches!(
         ip_addr.octets(),
-        [103, ..]
-            | [116, ..]
-            | [45, ..]
-            | [27, ..]
-            | [43, ..]
-            | [89, ..]
-            | [92, ..]
-            | [173, ..]
-            | [191, ..]
+        [2, 254, 141, ..]
+            | [1, 254, 163, ..]
+            | [1, 254, 216, ..]
+            | [1, 230, 27, ..]
+            | [1, 239, 251, ..]
+            | [1, 249, 35, ..]
+            | [1, 64, 184, ..]
+            | [4, 64, 185, ..]
+            | [4, 64, 186, ..]
+            | [4, 64, 187, ..]
+            | [4, 128, 25, ..]
+            | [4, 128, 26, ..]
+            | [8, 213, 177, ..]
+            | [9, 63, 180, ..]
+            | [1, 27, 200, ..]
+            | [1, 27, 201, ..]
+            | [1, 27, 202, ..]
+            | [1, 27, 203, ..]
+            | [1, 52, 108, ..]
+            | [1, 52, 109, ..]
+            | [1, 70, 7, ..]
+            | [1, 78, 31, ..]
+            | [1, 86, 48, ..]
+            | [1, 86, 49, ..]
+            | [1, 86, 50, ..]
+            | [1, 86, 51, ..]
+            | [1, 132, 0, ..]
+            | [1, 132, 1, ..]
+            | [1, 132, 3, ..]
+            | [1, 169, 67, ..]
+            | [1, 216, 158, ..]
+            | [1, 216, 159, ..]
+            | [1, 230, 120, ..]
+            | [1, 230, 121, ..]

AND This code don't have anything to do with clickhouse just filter some IP form kernel space via XDP BUT THIS MADE IT PANIC??

nuel77 commented 2 months ago

@slvrtrn , can you try calling the inserter.end() immedialty after starting the inserter.

    let mut inserter = client.insert(table).unwrap();
    inserter
        .end()
        .await
        .context("cannot commit inserter for table {:table?}")?;

this happened to us when the iter() was empty in my code.

vazw commented 2 months ago

@slvrtrn , can you try calling the inserter.end() immedialty after starting the inserter.

    let mut inserter = client.insert(table).unwrap();
    inserter
        .end()
        .await
        .context("cannot commit inserter for table {:table?}")?;

this happened to us when the iter() was empty in my code.

This might be the case I've try added is_empty() check before insert and it work now!