Skip to content

Concurrent reading from cell is not supported? #861

@listen-lavender

Description

@listen-lavender

Description

Steps to reproduce the issue:

  1. an Excel file with many picture cell
  2. need concurrent reading from cell
  3. check by go run -race

Test Excel
test_import_goods.xlsx

Test Code

package main

import (
  "strconv"
  "sync"

  "github.com/360EntSecGroup-Skylar/excelize/v2"
)

var tableIndex = map[int]string{
  1: "A", 2: "B", 3: "C", 4: "D", 5: "E",
  6: "F", 7: "G", 8: "H", 9: "I", 10: "J",
  11: "K", 12: "L", 13: "M", 14: "N", 15: "O",
  16: "P", 17: "Q", 18: "R", 19: "S", 20: "T",
  21: "U", 22: "V", 23: "W", 24: "X", 25: "Y", 26: "Z",
}

// 检查导入文件
func main() {
  localFile := "test_import_goods.xlsx"
  f, err := excelize.OpenFile(localFile)
  if err != nil {
    println("UploadGoods", "excelize open error", err.Error())
    return
  }

  var (
    sheet string
  )

  sheets := f.GetSheetList()
  if len(sheets) > 0 {
    sheet = sheets[0]
  } else {
    println("UploadGoods", "no sheet")
    return
  }

  var wg sync.WaitGroup

  sheetRows := getSheetRows(f, sheet) //行数量
  sheetCols := getSheetCols(f, sheet) //列数量

  // time.Sleep(time.Second * 3600)
  for i := 0; i < sheetRows; i++ {
    if i < 2 {
      continue
    }
    for j := 0; j < sheetCols; j++ {
      if j < 9 {
        continue
      }
      wg.Add(1)
      go func(i int, j int) {
        defer func() {
          if r := recover(); r != nil {
            println("ImportCheckImpl error")
          }
        }()
        defer wg.Done()
        if _, _, err := f.GetPicture(sheet, tableIndex[j+1]+strconv.Itoa(i+1)); err != nil {
          println("GetPicture", err.Error())
        }
      }(i, j)
    }
  }
  wg.Wait()
}

//读取行数
func getSheetRows(f *excelize.File, sheet string) int {
  i := 0
  rows, err := f.Rows(sheet)
  if err != nil {
    println("getSheetCols f.Rows", "excelize open error", err.Error())
    return i
  }
  for rows.Next() {
    //读取每行的数据
    cols, err := rows.Columns()
    if err != nil {
      println("getSheetCols rows.Columns()", "row", i, "get columns error", err.Error())
      continue
    }
    cData := ""
    for _, c := range cols {
      cData = cData + c
    }
    if cData == "" { //判断下一行是否为空数据(存在表格显示没有数据但是能读取出空数据的情况)
      break
    }
    i = i + 1
    continue
  }
  return i
}

//读取列数
func getSheetCols(f *excelize.File, sheet string) int {
  i := 0
  cols, err := f.Cols(sheet)
  if err != nil {
    println("getSheetCols f.Rows", "excelize open error", err.Error())
    return i
  }
  for cols.Next() {
    //读取每行的数据
    // cols, err := rcols.Columns()
    cols, err := cols.Rows()
    if err != nil {
      println("getSheetCols rows.Columns()", "row", i, "get columns error", err.Error())
      continue
    }
    cData := ""
    for _, c := range cols {
      cData = cData + c
    }
    if cData == "" { //判断下一行是否为空数据(存在表格显示没有数据但是能读取出空数据的情况)
      break
    }
    i = i + 1
    continue
  }
  return i
}

Describe the results you received:

Describe the results you expected:

Output of go version:

go version go1.14.4 darwin/amd64

Excelize version or commit ID:

Output of Error message:

==================
WARNING: DATA RACE
Write at 0x00c000519b90 by goroutine 28:
  runtime.mapassign_faststr()
      /usr/local/Cellar/go/1.14/src/runtime/map_faststr.go:202 +0x0
  github.com/360EntSecGroup-Skylar/excelize/v2.(*File).drawingParser()
      /usr/local/Cellar/go/1.14/selfcode/pkg/mod/github.com/360!ent!sec!group-!skylar/excelize/[email protected]/drawing.go:1165 +0xcf2
  github.com/360EntSecGroup-Skylar/excelize/v2.(*File).getPicture()
      /usr/local/Cellar/go/1.14/selfcode/pkg/mod/github.com/360!ent!sec!group-!skylar/excelize/[email protected]/picture.go:513 +0x6d
  github.com/360EntSecGroup-Skylar/excelize/v2.(*File).GetPicture()
      /usr/local/Cellar/go/1.14/selfcode/pkg/mod/github.com/360!ent!sec!group-!skylar/excelize/[email protected]/picture.go:478 +0x35b
  main.main.func1()
      /Users/lavenderuni/workspace/yunzujia/go/src/code.clouderwork.com/clouderwork/ycd_goods/test/test_import_goods.go:154 +0x20c

Previous read at 0x00c000519b90 by goroutine 29:
  runtime.mapaccess1_faststr()
      /usr/local/Cellar/go/1.14/src/runtime/map_faststr.go:12 +0x0
  github.com/360EntSecGroup-Skylar/excelize/v2.(*File).drawingParser()
      /usr/local/Cellar/go/1.14/selfcode/pkg/mod/github.com/360!ent!sec!group-!skylar/excelize/[email protected]/drawing.go:1141 +0x9c
  github.com/360EntSecGroup-Skylar/excelize/v2.(*File).getPicture()
      /usr/local/Cellar/go/1.14/selfcode/pkg/mod/github.com/360!ent!sec!group-!skylar/excelize/[email protected]/picture.go:513 +0x6d
  github.com/360EntSecGroup-Skylar/excelize/v2.(*File).GetPicture()
      /usr/local/Cellar/go/1.14/selfcode/pkg/mod/github.com/360!ent!sec!group-!skylar/excelize/[email protected]/picture.go:478 +0x35b
  main.main.func1()
      /Users/lavenderuni/workspace/yunzujia/go/src/code.clouderwork.com/clouderwork/ycd_goods/test/test_import_goods.go:154 +0x20c

Goroutine 28 (running) created at:
  main.main()
      /Users/lavenderuni/workspace/yunzujia/go/src/code.clouderwork.com/clouderwork/ycd_goods/test/test_import_goods.go:144 +0xa51

Goroutine 29 (running) created at:
  main.main()
      /Users/lavenderuni/workspace/yunzujia/go/src/code.clouderwork.com/clouderwork/ycd_goods/test/test_import_goods.go:144 +0xa51
==================
==================
WARNING: DATA RACE
Read at 0x00c000a10340 by goroutine 31:
  github.com/360EntSecGroup-Skylar/excelize/v2.(*File).getSheetRelationshipsTargetByID()
      /usr/local/Cellar/go/1.14/selfcode/pkg/mod/github.com/360!ent!sec!group-!skylar/excelize/[email protected]/picture.go:429 +0x20b
  github.com/360EntSecGroup-Skylar/excelize/v2.(*File).GetPicture()
      /usr/local/Cellar/go/1.14/selfcode/pkg/mod/github.com/360!ent!sec!group-!skylar/excelize/[email protected]/picture.go:469 +0x191
  main.main.func1()
      /Users/lavenderuni/workspace/yunzujia/go/src/code.clouderwork.com/clouderwork/ycd_goods/test/test_import_goods.go:154 +0x20c

Previous write at 0x00c000a10340 by goroutine 28:
  reflect.typedmemmove()
      /usr/local/Cellar/go/1.14/src/runtime/mbarrier.go:177 +0x0
  reflect.Value.Set()
      /usr/local/Cellar/go/1.14/src/reflect/value.go:1534 +0xfb
  reflect.Append()
      /usr/local/Cellar/go/1.14/src/reflect/value.go:2028 +0x151
  encoding/xml.(*Decoder).unmarshal()
      /usr/local/Cellar/go/1.14/src/encoding/xml/read.go:398 +0x3552
  encoding/xml.(*Decoder).unmarshalPath()
      /usr/local/Cellar/go/1.14/src/encoding/xml/read.go:690 +0x90c
  encoding/xml.(*Decoder).unmarshal()
      /usr/local/Cellar/go/1.14/src/encoding/xml/read.go:524 +0x16a7
  encoding/xml.(*Decoder).DecodeElement()
      /usr/local/Cellar/go/1.14/src/encoding/xml/read.go:151 +0x11e
  encoding/xml.(*Decoder).Decode()
      /usr/local/Cellar/go/1.14/src/encoding/xml/read.go:139 +0x438
  github.com/360EntSecGroup-Skylar/excelize/v2.(*File).relsReader()
      /usr/local/Cellar/go/1.14/selfcode/pkg/mod/github.com/360!ent!sec!group-!skylar/excelize/[email protected]/sheet.go:1617 +0x44d
  github.com/360EntSecGroup-Skylar/excelize/v2.(*File).getSheetRelationshipsTargetByID()
      /usr/local/Cellar/go/1.14/selfcode/pkg/mod/github.com/360!ent!sec!group-!skylar/excelize/[email protected]/picture.go:425 +0x19e
  github.com/360EntSecGroup-Skylar/excelize/v2.(*File).GetPicture()
      /usr/local/Cellar/go/1.14/selfcode/pkg/mod/github.com/360!ent!sec!group-!skylar/excelize/[email protected]/picture.go:469 +0x191
  main.main.func1()
      /Users/lavenderuni/workspace/yunzujia/go/src/code.clouderwork.com/clouderwork/ycd_goods/test/test_import_goods.go:154 +0x20c

Goroutine 31 (running) created at:
  main.main()
      /Users/lavenderuni/workspace/yunzujia/go/src/code.clouderwork.com/clouderwork/ycd_goods/test/test_import_goods.go:144 +0xa51

Goroutine 28 (running) created at:
  main.main()
      /Users/lavenderuni/workspace/yunzujia/go/src/code.clouderwork.com/clouderwork/ycd_goods/test/test_import_goods.go:144 +0xa51
==================
==================
WARNING: DATA RACE
Read at 0x00c00071e178 by goroutine 89:
  github.com/360EntSecGroup-Skylar/excelize/v2.(*File).drawingParser()
      /usr/local/Cellar/go/1.14/selfcode/pkg/mod/github.com/360!ent!sec!group-!skylar/excelize/[email protected]/drawing.go:1168 +0xdae
  github.com/360EntSecGroup-Skylar/excelize/v2.(*File).getPicture()
      /usr/local/Cellar/go/1.14/selfcode/pkg/mod/github.com/360!ent!sec!group-!skylar/excelize/[email protected]/picture.go:513 +0x6d
  github.com/360EntSecGroup-Skylar/excelize/v2.(*File).GetPicture()
      /usr/local/Cellar/go/1.14/selfcode/pkg/mod/github.com/360!ent!sec!group-!skylar/excelize/[email protected]/picture.go:478 +0x35b
  main.main.func1()
      /Users/lavenderuni/workspace/yunzujia/go/src/code.clouderwork.com/clouderwork/ycd_goods/test/test_import_goods.go:154 +0x20c

Previous write at 0x00c00071e178 by goroutine 33:
  [failed to restore the stack]

Goroutine 89 (running) created at:
  main.main()
      /Users/lavenderuni/workspace/yunzujia/go/src/code.clouderwork.com/clouderwork/ycd_goods/test/test_import_goods.go:144 +0xa51

Goroutine 33 (running) created at:
  main.main()
      /Users/lavenderuni/workspace/yunzujia/go/src/code.clouderwork.com/clouderwork/ycd_goods/test/test_import_goods.go:144 +0xa51
==================

Environment details (OS, Microsoft Excel™ version, physical, etc.):

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions