Error: Panic: runtime error: invalid memory address or nil pointer dereference

Hi Couchbase Gurus,

Need some help here. I encountered this error on my query:
Error: Panic: runtime error: invalid memory address or nil pointer dereference

SELECT
  brands AS brand,
  departments AS department,
  ranks AS `rank`,
  ARRAY_SORT( ARRAY_DISTINCT( ARRAY_FLATTEN( category, 1 ) ) ) AS categories,
  tags
  LET 
    brands = (
      SELECT DISTINCT RAW LOWER(brand)
      FROM `bucket`
      WHERE `type` = 'product' AND brand IS NOT MISSING
      AND status = 'active' AND tenant = 'tenant::bucket'
      ORDER BY LOWER(brand) ASC
    ),
    departments = (
      SELECT DISTINCT RAW department
      FROM `bucket`
      WHERE `type` = 'product' AND department IS NOT MISSING
      AND status = 'active' AND tenant = 'tenant::bucket'
      ORDER BY(department) ASC
    ),
    ranks = (
      SELECT DISTINCT RAW `rank`.level
      FROM `bucket`
      WHERE `type` = 'product' AND `rank`.level IS NOT MISSING
      AND status = 'active' AND tenant = 'tenant::bucket'
      ORDER BY(`rank`.level) ASC
    ),
    category = (
      SELECT DISTINCT RAW categories
      FROM `bucket`
      WHERE `type` = 'product' AND categories IS NOT MISSING
      AND status = 'active' AND tenant = 'tenant::bucket'
    ),
    tags = (
      SELECT DISTINCT RAW tag
      FROM `bucket` AS b
      UNNEST b.tags AS tag
      WHERE b.`type` = 'product' AND b.tags IS NOT MISSING
      AND b.status = 'active' AND b.tenant = 'tenant::bucket'
      ORDER BY LOWER(tag)
    )

Hi @lyndondonz ,

Check query.log ( path is listed here Manage Logging | Couchbase Docs). There is panic and stack trace paste that info.

Instead of fetching every query you can try the following options and see how it performs.

WITH data AS (SELECT RAW d FROM `bucket` AS d
WHERE `type` = 'product' AND status = 'active' AND tenant = 'tenant::bucket')
SELECT
  brands AS brand,
  departments AS department,
  ranks AS `rank`,
  ARRAY_SORT( ARRAY_DISTINCT( ARRAY_FLATTEN( category, 1 ) ) ) AS categories,
  tags
  LET 
    brands = (
      SELECT DISTINCT RAW LOWER(brand)
      FROM data AS d1
      WHERE `type` = 'product' AND brand IS NOT MISSING
      AND status = 'active' AND tenant = 'tenant::bucket'
      ORDER BY LOWER(brand) ASC
    ),
    departments = (
      SELECT DISTINCT RAW department
      FROM data AS d1
      WHERE `type` = 'product' AND department IS NOT MISSING
      AND status = 'active' AND tenant = 'tenant::bucket'
      ORDER BY(department) ASC
    ),
    ranks = (
      SELECT DISTINCT RAW `rank`.level
      FROM data AS d1
      WHERE `type` = 'product' AND `rank`.level IS NOT MISSING
      AND status = 'active' AND tenant = 'tenant::bucket'
      ORDER BY(`rank`.level) ASC
    ),
    category = (
      SELECT DISTINCT RAW categories
      FROM data AS d1
      WHERE `type` = 'product' AND categories IS NOT MISSING
      AND status = 'active' AND tenant = 'tenant::bucket'
    ),
    tags = (
      SELECT DISTINCT RAW tag
      FROM data AS b
      UNNEST b.tags AS tag
      WHERE b.`type` = 'product' AND b.tags IS NOT MISSING
      AND b.status = 'active' AND b.tenant = 'tenant::bucket'
      ORDER BY LOWER(tag)
    )

OR

SELECT ARRAY_SORT(ARRAY_AGG (DISTINCT LOWER(brand))) AS brands,
       ARRAY_SORT(ARRAY_AGG (DISTINCT department)) AS department,
       ARRAY_SORT(ARRAY_AGG (DISTINCT `rank`.level)) AS `rank`,
       ARRAY_SORT(ARRAY_DISTINCT(ARRAY_FLATTEN(ARRAY_AGG (ARRAY LOWER(v) FOR v IN tags END),1))) AS tags,
       ARRAY_SORT(ARRAY_DISTINCT(ARRAY_FLATTEN(ARRAY_AGG (categories),1))) AS categories
FROM `bucket` AS d
WHERE `type` = 'product' AND status = 'active' AND tenant = 'tenant::bucket'

Hi @vsr1,
I can’t find the logs ( panic and stack trace) about the Error: Panic: runtime error: invalid memory address or nil pointer dereference

It only throws error in our API intermittently. But, when I try query string on the CouchBase Server it runs correctly with no errors. Do you have any idea why this occur? By the way we’re use NodeJS SDK.

Btw, I will try your suggested N1QL query. I hope this would resolved the issue.

If you have more than one query node, check all the nodes (As it depends on which query node it ran the query). (search for panic) panic normally writes in /opt/couchbase/var/lib/couchbase/logs/query.log (if there is compressed query.log check those, too much messages might moved there)

Hi @vsr1,

I’ve just replicated the issue on my couchbase console.

It sometimes return an empty results.

Do you think It was my query? no WHERE clause on it ?

thats the error generate on the Couchbase logs

Chrome/90.0.4430.93 Safari/537.36
Got unhandled javascript error:
message: Script error.;

No WHERE clause should be ok. What is version of couchbase.
Try remove one let at a time and see which one causing.
If using 6.5.0 might hitting MB-37365. Add the FROM 1 AS dd before LET

Hi @vsr1 ,

Same thing, the error still occurs:

I’ve just query the brands. Also, try the other properties still got the error.

Hi @lyndondonz ,

If using 6.5.0 might hitting MB-37365.
Try this.

SELECT
  brands AS brand,
  departments AS department,
  ranks AS `rank`,
  ARRAY_SORT( ARRAY_DISTINCT( ARRAY_FLATTEN( category, 1 ) ) ) AS categories,
  tags
FROM 1 AS dd
  LET 
    brands = (
      SELECT DISTINCT RAW LOWER(brand)
      FROM `bucket`
      WHERE `type` = 'product' AND brand IS NOT MISSING
      AND status = 'active' AND tenant = 'tenant::bucket'
      ORDER BY LOWER(brand) ASC
    ),
    departments = (
      SELECT DISTINCT RAW department
      FROM `bucket`
      WHERE `type` = 'product' AND department IS NOT MISSING
      AND status = 'active' AND tenant = 'tenant::bucket'
      ORDER BY(department) ASC
    ),
    ranks = (
      SELECT DISTINCT RAW `rank`.level
      FROM `bucket`
      WHERE `type` = 'product' AND `rank`.level IS NOT MISSING
      AND status = 'active' AND tenant = 'tenant::bucket'
      ORDER BY(`rank`.level) ASC
    ),
    category = (
      SELECT DISTINCT RAW categories
      FROM `bucket`
      WHERE `type` = 'product' AND categories IS NOT MISSING
      AND status = 'active' AND tenant = 'tenant::bucket'
    ),
    tags = (
      SELECT DISTINCT RAW tag
      FROM `bucket` AS b
      UNNEST b.tags AS tag
      WHERE b.`type` = 'product' AND b.tags IS NOT MISSING
      AND b.status = 'active' AND b.tenant = 'tenant::bucket'
      ORDER BY LOWER(tag)
    )

Hi @vsr1 ,

yeah, your query seems to work with FROM 1 AS dd and that is pretty weird. I will read more on the issue MB-37365.

Well, I just have to re-write my N1QL query, base you’ve suggested earlier on you post :slight_smile:

Thanks Man.

basically it is double freeing. You can upgrade latest version. or Add dummy FROM clause (don’t use LET without FROM clause). After change query try kill cbq-engine process (it will restart), so that you don’t see this unexpected error some other scenario.