Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"An item with the same key has already been added." when Parallel code #1662

Closed
2 of 6 tasks
doylecnn opened this issue Apr 30, 2021 · 3 comments
Closed
2 of 6 tasks

Comments

@doylecnn
Copy link

doylecnn commented Apr 30, 2021

Read and complete the full issue template

Do not randomly delete sections. They are here for a reason.

Do you want to request a feature or report a bug?

  • Bug
  • Feature
  • Question

Did you test against the latest CI build?

  • Yes
  • No

If you answered No, please test with the latest development build first.

Version of ClosedXML

0.95.4 and latest CI build (0.95.999.2311)

What is the current behavior?

get exception: An item with the same key has already been added. Key: 1, An item with the same key has already been added. Key: 20

What is the expected behavior or new feature?

no exception

Is this a regression from the previous version?

No

Reproducibility

Code to reproduce problem:

public void Main()
{
    var x = new ClosedXML.Excel.XLWorkbook();
    var s = x.AddWorksheet();
    var r = s.Row(1);
	System.Threading.Tasks.Parallel.For(1, 1000000, i => {
		var c1 = r.Cell(20);
		c1.SetValue(i);
	});
	Console.ReadLine();
}

the stack trace from inner exception:

   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at ClosedXML.Excel.XLCellsCollection.IncrementUsage(Dictionary`2 dictionary, Int32 key) in C:\projects\closedxml\ClosedXML\Excel\Cells\XLCellsCollection.cs:line 54
   at ClosedXML.Excel.XLCellsCollection.Add(Int32 row, Int32 column, XLCell cell) in C:\projects\closedxml\ClosedXML\Excel\Cells\XLCellsCollection.cs:line 33
   at ClosedXML.Excel.XLRangeBase.Cell(XLAddress& cellAddressInRange) in C:\projects\closedxml\ClosedXML\Excel\Ranges\XLRangeBase.cs:line 920
   at ClosedXML.Excel.XLRangeBase.Cell(Int32 row, Int32 column) in C:\projects\closedxml\ClosedXML\Excel\Ranges\XLRangeBase.cs:line 848
   at ClosedXML.Excel.XLRow.Cell(Int32 columnNumber) in C:\projects\closedxml\ClosedXML\Excel\Rows\XLRow.cs:line 144

lead me to:

  • I attached a sample spreadsheet. (You can drag files on to this issue)
@igitur
Copy link
Member

igitur commented Apr 30, 2021

ClosedXML isn't thread-safe.

@kenleese
Copy link

Suggestion: Add “ClosedXML is not thread-safe” to a “Limitations” section of the project readme.

@igitur @Pankraty thank you for your ongoing efforts on this awesome project

@igitur igitur changed the title get exception: "An item with the same key has already been added." when Parallel code "An item with the same key has already been added." when Parallel code May 1, 2021
@igitur
Copy link
Member

igitur commented May 1, 2021

Suggestion: Add “ClosedXML is not thread-safe” to a “Limitations” section of the project readme.

Frequent answers section added to the README.

@igitur igitur closed this as completed May 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants