Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Possible bug? Why does running Mata interactively after the pause command throw r(3000) errors?

    In Stata 13.1 for Windows x64, turning -pause on-, then running -pause- followed by -mata- in the interactive terminal, displays an infinite scrolling list of r(3000) errors. There is no way to cancel execution without killing the Stata process externally.

    Is this a bug?

    To be clear, the code (which isn't in a do-file, but executed interactively) is

    Code:
    pause on
    pause
    mata
    executed one line at a time in the interactive window.

  • #2
    Hello Michael, hello list,

    I can confirm this running Stata 13.1 for Linux (64bit) on an Ubuntu machine. The problem seems to be that Stata is unable to jump into Mata mode when paused.

    As soon as pause mode is stopped before starting Mata, everything seems to work as expected:
    Code:
    pause on
    pause
    q
    mata
    But naturally this is not what Michael wants to achieve.
    If it is technically impossible to jump into Mata when Stata is paused, Mata should quit with an appropriate error code (once), I think.

    Regards
    Bela

    Comment


    • #3
      Hi Daniel,

      Thanks for confirming that; hopefully this will be explained or corrected soon.

      Comment


      • #4
        Hopefully more experienced members on the list, and/or people from Statacorp, will comment on whether or not this behaviour is intended or if this is actually a bug.

        Comment


        • #5
          Can anyone else besides Daniel and I reproduce this? Is this actually a bug in Stata?

          Comment


          • #6
            This is a bug in pause. Pause should not allow Mata to be started and should produce an error instead of an infinite loop. We will fix this in a future update.

            Comment


            • #7
              Why shouldn't pause allow Mata to be started? What if I want to pause execution of a do-file and use scalars, variables, etc. in the current scope in Mata interactively? It seems like simply disabling Mata from working while execution is paused is very much the wrong solution.

              Comment


              • #8
                Michael, note that Mata one-liners still work:

                Code:
                . sysuse cancer
                (Patient Survival in Drug Trial)
                
                . pause on
                
                . pause
                pause:  
                -> . mata: X = st_data(., .)
                -> . mata: X
                         1    2    3    4    5    6    7    8
                     +-----------------------------------------+
                   1 |   1    1    1   61    1    1    1    0  |
                   2 |   1    1    1   65    1    1    1    0  |
                 ...
                  48 |  39    0    3   52    1    0   39    0  |
                     +-----------------------------------------+
                -> . end
                execution resumes...
                Interactive use of Mata isn't the only thing that pause doesn't support: Stata blocks are also unsupported, including for-loops and if blocks.

                If you need Mata blocks (if, for, ...) and think that means a one-liner won't do, note that many blocks can be converted to ugly-one-liners. For instance, in the example above, you could display only rows of X such that the first column is 1. A pretty block might be:

                Code:
                for (i = 1; i <= rows(X); i++)
                    if (X[i, 1] == 1)
                        X[i,]
                pause won't accept this as-is, but you can convert it to the following one-liner:

                Code:
                . sysuse cancer
                (Patient Survival in Drug Trial)
                
                . pause on
                
                . pause
                pause:  
                -> . mata: X = st_data(., .)
                -> . mata: for (i = 1; i <= rows(X); i++) if (X[i, 1] == 1) X[i,];;
                        1    2    3    4    5    6    7    8
                    +-----------------------------------------+
                  1 |   1    1    1   61    1    1    1    0  |
                    +-----------------------------------------+
                        1    2    3    4    5    6    7    8
                    +-----------------------------------------+
                  1 |   1    1    1   65    1    1    1    0  |
                    +-----------------------------------------+
                -> . end
                execution resumes...
                (Of course, the Mata select() function is better than this loop, which is meant only for example purposes.)

                Comment


                • #9
                  Is this bug going to be fixed in the next Stata 13 update, or at least in Stata 14?

                  Comment


                  • #10
                    Originally posted by Michael Anbar
                    Why shouldn't pause allow Mata to be started? What if I want to pause execution of a do-file and use scalars, variables, etc. in the current scope in Mata interactively? It seems like simply disabling Mata from working while execution is paused is very much the wrong solution.

                    Is this bug going to be fixed in the next Stata 13 update, or at least in Stata 14?
                    There is an update to Stata 13 going out today which will NOT have the code in it to prevent an attempt to launch an interactive Mata session from within pause. That check will go in the next update after today's update. Likewise, this check is not in Stata 14 but will be in the first update to Stata 14.

                    The reason pause can't be modified to allow Mata to be launched in interactive mode is because pause is an ado-file. When pause tries to execute mata, Mata indeed launches. But, because it has been called from an ado-file, it is NOT really in interactive mode. Instead, it starts "eating" lines out of the ado-file, trying to execute them. They all result in error, and eventually it reaches the end of the ado-file. Before all of that happened, Stata was executing a while loop to ask the user to type commands and then execute them "interactively". Once Stata/Mata reaches the end of the ado-file (because the "interactive" mata ate all the remaining lines in the ado-file), Stata has a failsafe which kicks in for loops in case a user didn't terminate them. This failsafe kicks in, and execution returns to the beginning of the while loop.

                    You can see what is going on if you set trace on before starting pause and then typing mata. I don't recommend doing that, however, as you will indeed be stuck in an endless loop and will have to kill the process externally.

                    In any case, in the next set of updates, we'll include the code to prevent this from happening.

                    Comment


                    • #11
                      Originally posted by Alan Riley (StataCorp) View Post
                      Instead, it starts "eating" lines out of the ado-file, trying to execute them.
                      Dear Alan,
                      Is this the reason why multiline mata doesn't work inside a loop?
                      It seems for loops are broken with an undocumented -end- command, but if Mata uses something like _request2() then I can imagine -end- is not the underlying issue?

                      I wonder because there've been a few efforts to add Python/R support within Stata as constructs that use _request2() to enhance the language, but they all suffer from not being able to run within a loop, or within a program.

                      It would be *huge* to find a way where _request2 or anything similar works within a loop/program, so then we can properly use python/r/etc within the Stata language.

                      Comment

                      Working...
                      X