Nim icon indicating copy to clipboard operation
Nim copied to clipboard

SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Open alialrahahleh opened this issue 5 months ago • 5 comments

import os, streams, parsexml, strutils, xmldomparser, xmldom, sequtils, cpuinfo import itertools import taskpools import sugar import zip/gzipfiles import std/tables

proc qoute(s: string): string = if s.map(x => x.isDigit).all(x => x) and s[0] != '0' and s[0] != '-' and s[0] != '+': return s else: return """ & s.replace("\", "\\").replace(""", "\"") & """

proc isArray(childNodes: seq[PNode]): bool = if childNodes.len <= 1: return false

return childNodes.all(x => x.nodeName == childNodes[0].nodeName and x.nodeType == ElementNode)

proc processElement(ele: PNode, cache: var Table[string, bool]): string {.gcsafe.} = result = "" let isArray = cache.getOrDefault(ele.nodeName) or isArray(ele.childNodes) or ele.nodeName.endsWith("s") cache[ele.nodeName] = isArray

if ele.nodeType == TextNode:
  return qoute(ele.nodeValue)

var attr = initTable[string, string]()

for x in ele.attributes:
  attr[x.nodeName] = x.value

var list = newSeq[string]()


if attr.len > 0 and not isArray:
  for k, v in attr:
    list.add(qoute(k) & " : " &  qoute(v))

  for x in ele.childNodes:
    if x.nodeType == TextNode:
      list.add("\"value\" :" & processElement(x, cache))
    else:
      list.add(qoute(x.nodeName) & ":" & processElement(x, cache))

  result.add("{") 
  result.add(list.join(","))
  result.add("}")

elif isArray:
  for x in ele.childNodes:
    list.add(processElement(x, cache))


  result.add("[")
  result.add(list.join(","))
  result.add("]")

elif ele.childNodes.len > 1:
  for x in ele.childNodes:
    if x.nodeType == TextNode:
      list.add("\"value\" :" & processElement(x, cache))
    else:
      list.add(qoute(x.nodeName) & ":" & processElement(x, cache))

  result.add("{") 
  result.add(list.join(","))
  result.add("}")

elif ele.childNodes.len == 1:
  if ele.childNodes[0].nodeType == TextNode:
    result.add(processElement(ele.childNodes[0], cache))
  else:
    result.add("{" & qoute(ele.childNodes[0].nodeName) & " : " & processElement(ele.childNodes[0], cache) & "}")
else:
  result.add("{}")

proc processor(lines: seq[string]): seq[string] = var cache = initTablestring, bool for line in lines: let node = loadXML(line) let ele = node.documentElement result.add(processElement(ele, cache))

proc stIterator(s: GzFileStream): iterator(): string = return iterator(): string = while true: let line = s.readLine() if line.contains("<program "): yield line

proc job() = var nthreads = countProcessors() var tp = Taskpool.new(num_threads = nthreads) var outStream = newGzFileStream("out.json.gz", fmWrite) var s = newGzFileStream("/tmp/xxx.xml.gz", fmRead) var buffer = newSeqstring var tasks = newSeqFlowVar[seq[string]] let gen = stIterator(s)

for item in gen:

buffer.add(item)
if buffer.len == 10000:
  let k = tp.spawn processor(buffer)
  tasks.add(k)
  echo "spawn task"
  echo tasks
  buffer = newSeq[string]()

if tasks.len == 4:
  echo "flushing"
  tp.syncAll()
  for x in tasks:
    let result = sync x
    for r in result:
      outStream.writeLine(r)

  tasks = newSeq[FlowVar[seq[string]]]()

tp.shutdown()

when isMainModule:

job()

Nim Version

2.2 and 1.6.18

Current Output

SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Error: execution of an external program failed: '/Users/aalrahahleh/.cache/nim/parser_r/parser_4F266EA21321037C819FD82E08E486710A12F226 '

Expected Output

No response

Possible Solution

No response

Additional Information

No response

alialrahahleh avatar Feb 05 '24 22:02 alialrahahleh