Skip to content

ShellStream.Expect(Regex, TimeSpan) can return "too much" string #454

Closed

Description

@keithallenjackson

ShellStream.Expect(Regex, TimeSpan) properly matches and dequeues data from the _incoming data queue. However, once a match is made, instead of returning all text up to the point of a match, it returns all data within the queue.

Current:

public string Expect(Regex regex, TimeSpan timeout)
        {
           var text = string.Empty;

            while (true)
            {
                lock (_incoming)
                {
                    if (_incoming.Count > 0)
                    {
                        text = _encoding.GetString(_incoming.ToArray(), 0, _incoming.Count);
                    }

                    var match = regex.Match(text);

                    if (match.Success)
                    {
                        //  Remove processed items from the queue
                        for (var i = 0; i < match.Index + match.Length && _incoming.Count > 0; i++)
                        {
                            _incoming.Dequeue();
                        }
                        break;
                    }
                }

               if (timeout.Ticks > 0)
                    {
                        if (!_dataReceived.WaitOne(timeout))
                        {
                            return null;
                        }
                    }
                    else
                    {
                        _dataReceived.WaitOne();
                    }
           }

            return text;
        }

Fix:

public string Expect(Regex regex, TimeSpan timeout)
        {
           var text = string.Empty;

            while (true)
            {
                lock (_incoming)
                {
                    if (_incoming.Count > 0)
                    {
                        text = _encoding.GetString(_incoming.ToArray(), 0, _incoming.Count);
                    }

                    var match = regex.Match(text);

                    if (match.Success)
                    {
                        //  Remove processed items from the queue
                        for (var i = 0; i < match.Index + match.Length && _incoming.Count > 0; i++)
                        {
                            _incoming.Dequeue();
                        }
                        text = text.Substring(0, match.Index + match.Length); // <-- This one line of code fixes it!
                        break;
                    }
                }

               if (timeout.Ticks > 0)
                    {
                        if (!_dataReceived.WaitOne(timeout))
                        {
                            return null;
                        }
                    }
                    else
                    {
                        _dataReceived.WaitOne();
                    }
           }

            return text;
        }

Activity

  1. added 2 commits that reference this issue on Aug 12, 2018
  2. IgorMilavec commented on Feb 24, 2022

    @IgorMilavec
    Collaborator

    I have created a discussion #926 for all ShellStream related issues.

  3. IgorMilavec commented on Mar 21, 2022

    @IgorMilavec
    Collaborator

    @keithallenjackson I'm trying to better understand usage patterns related to Expect API. Can you please explain how you use it and if possible provide a proto code for it?

  4. jscarle commented on Feb 13, 2024

    @jscarle
    Contributor

    @IgorMilavec @WojciechNagorski This was fixed by #1207 and #1319.

  5. WojciechNagorski commented on Feb 22, 2024

    @WojciechNagorski
    Collaborator

    This issue has been fixed in the 2024.0.0 version.

  6. added this to the 2024.0.0 milestone on Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions