Bug 5304 - ConcurrentStack.TryPopRange() doesn't support arrays of length 1
Summary: ConcurrentStack.TryPopRange() doesn't support arrays of length 1
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: master
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Jérémie Laval
Depends on:
Reported: 2012-05-24 11:11 UTC by Jonathan Pryor
Modified: 2012-05-24 15:57 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Jonathan Pryor 2012-05-24 11:11:58 UTC
ConcurrentStack.TryPopRange() doesn't support arrays of length 1. Consider the following app:

  // popping ranges of length 1 doesn't work
  using System;
  using System.Collections.Concurrent;

  class Test {
    public static void Main ()
      var s = new ConcurrentStack<int> ();
      s.PushRange (new []{5,4,3,2,1});
      int[] b = new int[1];
      int n;
      if ((n = s.TryPopRange (b)) > 0) {
        Console.WriteLine ("Popped range! n={0}; contents: {1}",
            n, string.Join (", ", b));
      Console.WriteLine ("s contents: {0}",
          string.Join (", ", s));
      if (n == 0)
        throw new Exception ("Shouldn't we have popped one element?");

Compile & run:

  $ dmcs constack.cs
  $ mono constack.exe

Expected output/.NET output:

  Popped range! n=1; contents: 1
  s contents: 2, 3, 4, 5

mono 2.10.9 output:

  s contents: 1, 2, 3, 4, 5

  Unhandled Exception: System.Exception: Shouldn't we have popped one element?
    at Test.Main () [0x00000] in <filename unknown>:0
Comment 1 Jonathan Pryor 2012-05-24 11:36:59 UTC
Something must be horribly wrong on my mono/master build:

  $ mono --version
  Mono JIT compiler version 2.11.2 (master/f78e6f8 Thu May 24 11:11:17 EDT 2012)
  Copyright (C) 2002-2012 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
    TLS:           normal
    SIGSEGV:       altstack
    Notification:  kqueue
    Architecture:  x86
    Disabled:      none
    Misc:          softdebug 
    LLVM:          supported, not enabled.
    GC:            Included Boehm (with typed GC)
  $ dmcs constack.cs
  $ mono constack.exe 
  s contents: 0

  Unhandled Exception: System.Exception: Shouldn't we have popped one element?
    at Test.Main () [0x00000] in <filename unknown>:0 

Why isn't anything being added to the ConcurrentStack<int>?

Further investigation shows that PushRange() is working, the problem is that TryPopRange() is clearing the entire stack! (Wow!) This looks like a major regression from 2.10.

Also, changing the length of the `b` array to 3 changes the output, but still clears out the stack:

  $ mono constack.exe 
  Popped range! n=2; contents: 1, 2, 3
  s contents: 0

(Aside: Where is "0" coming from? s.Count==0 afterward, so this should be an empty list. "0" shouldn't be showing up anywhere. This ALSO works correctly in 2.10.9, and appears to be a regression in master.)
Comment 2 Jérémie Laval 2012-05-24 14:07:05 UTC
Fixed in 40a84cb

A mixed bag of indexes mess and memory management problems.
Comment 3 Jonathan Pryor 2012-05-24 15:57:11 UTC
Please backport to mono-2-10.