Skip to content

Commit

Permalink
fixed #689 potential race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed Aug 15, 2020
1 parent c89ee42 commit b873376
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ env:

script:
- env GO111MODULE=on go vet ./...
- env GO111MODULE=on go test ./... -v -race -coverprofile=coverage.txt -covermode=atomic
- env GO111MODULE=on go test -v -race ./... -coverprofile=coverage.txt -covermode=atomic

after_success:
- bash <(curl -s https://codecov.io/bash)
14 changes: 2 additions & 12 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,6 @@ func setCellDuration(value time.Duration) (t string, v string) {
// SetCellInt provides a function to set int type value of a cell by given
// worksheet name, cell coordinates and cell value.
func (f *File) SetCellInt(sheet, axis string, value int) error {
f.Lock()
defer f.Unlock()
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return err
Expand All @@ -201,8 +199,6 @@ func setCellInt(value int) (t string, v string) {
// SetCellBool provides a function to set bool type value of a cell by given
// worksheet name, cell name and cell value.
func (f *File) SetCellBool(sheet, axis string, value bool) error {
f.Lock()
defer f.Unlock()
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return err
Expand Down Expand Up @@ -236,8 +232,6 @@ func setCellBool(value bool) (t string, v string) {
// f.SetCellFloat("Sheet1", "A1", float64(x), 2, 32)
//
func (f *File) SetCellFloat(sheet, axis string, value float64, prec, bitSize int) error {
f.Lock()
defer f.Unlock()
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return err
Expand All @@ -259,19 +253,15 @@ func setCellFloat(value float64, prec, bitSize int) (t string, v string) {
// SetCellStr provides a function to set string type value of a cell. Total
// number of characters that a cell can contain 32767 characters.
func (f *File) SetCellStr(sheet, axis, value string) error {
f.Lock()
xlsx, err := f.workSheetReader(sheet)
if err != nil {
f.Unlock()
return err
}
cellData, col, _, err := f.prepareCell(xlsx, sheet, axis)
if err != nil {
f.Unlock()
return err
}
cellData.S = f.prepareCellStyle(xlsx, col, cellData.S)
f.Unlock()
cellData.T, cellData.V = f.setCellString(value)
return err
}
Expand Down Expand Up @@ -372,8 +362,6 @@ type FormulaOpts struct {
// SetCellFormula provides a function to set cell formula by given string and
// worksheet name.
func (f *File) SetCellFormula(sheet, axis, formula string, opts ...FormulaOpts) error {
f.Lock()
defer f.Unlock()
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return err
Expand Down Expand Up @@ -698,6 +686,8 @@ func (f *File) SetSheetRow(sheet, axis string, slice interface{}) error {

// getCellInfo does common preparation for all SetCell* methods.
func (f *File) prepareCell(xlsx *xlsxWorksheet, sheet, cell string) (*xlsxC, int, int, error) {
xlsx.Lock()
defer xlsx.Unlock()
var err error
cell, err = f.mergeCellsParser(xlsx, cell)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestConcurrency(t *testing.T) {
wg.Add(1)
go func(val int) {
f.SetCellValue("Sheet1", fmt.Sprintf("A%d", val), val)
f.SetCellValue("Sheet1", fmt.Sprintf("B%d", val), strconv.Itoa(val))
f.GetCellValue("Sheet1", fmt.Sprintf("A%d", val))
wg.Done()
}(i)
Expand Down
2 changes: 2 additions & 0 deletions excelize.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ func (f *File) setDefaultTimeStyle(sheet, axis string, format int) error {
// workSheetReader provides a function to get the pointer to the structure
// after deserialization by given worksheet name.
func (f *File) workSheetReader(sheet string) (xlsx *xlsxWorksheet, err error) {
f.Lock()
defer f.Unlock()
var (
name string
ok bool
Expand Down

0 comments on commit b873376

Please sign in to comment.