Last modified: 2013-11-15 11:10:19 UTC

Wikimedia Bugzilla is closed!

Wikimedia migrated from Bugzilla to Phabricator. Bug reports are handled in Wikimedia Phabricator.
This static website is read-only and for historical purposes. It is not possible to log in and except for displaying bug reports and their history, links might be broken. See T48294, the corresponding Phabricator task for complete and up-to-date bug report information.
Bug 46294 - "Lua error: Unable to decode message" for any string that contains explicit \n char (newline) on Windows
"Lua error: Unable to decode message" for any string that contains explicit \...
Status: RESOLVED FIXED
Product: MediaWiki extensions
Classification: Unclassified
Scribunto (Other open bugs)
master
All Windows Vista
: Low normal (vote)
: ---
Assigned To: Brad Jorsch
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2013-03-18 19:07 UTC by Codicorumus
Modified: 2013-11-15 11:10 UTC (History)
5 users (show)

See Also:
Web browser: ---
Mobile Platform: ---
Assignee Huggle Beta Tester: ---


Attachments

Description Codicorumus 2013-03-18 19:07:51 UTC
On Windows Vista, I get 

"Lua error: Internal error: Unable to decode message."

for any string that contains explicit \n char, the same for multiline strings with long-brackets and for mw.log().


I've found an explication and an almost working fix in this thread on mediawiki.org :

//www.mediawiki.org/wiki/Thread:Extension_talk:Scribunto/Newline_encoding

see there for details. After changed to the better replace (that I'll post there after I get the bug number here), I've not seen any bug so far. Obviously it's not a cross-platform solution, but maybe could be a hint to one.
Comment 1 Brad Jorsch 2013-03-18 20:18:14 UTC
Try changing the flags in the proc_open call in LuaStandaloneEngine.php from 'r' and 'w' to 'rb' and 'wb', to specify binary mode.

It's not documented at http://www.php.net/proc_open, but it seems to have been in the PHP source since 2002.
Comment 2 Codicorumus 2013-03-18 21:10:00 UTC
(In reply to comment #1)
> Try changing the flags in the proc_open call in LuaStandaloneEngine.php from
> 'r' and 'w' to 'rb' and 'wb', to specify binary mode.
> 
> It's not documented at http://www.php.net/proc_open, but it seems to have
> been
> in the PHP source since 2002.

Flag change tried. I get the same error.
Comment 3 gnosygnu 2013-03-23 22:31:34 UTC
I just want to confirm the bug on my Windows XP machine.

--------------
Simple example
--------------

* Create Module:Bugs

* Add the following text

local z = {}

function z.new_line( frame )
    local s = "a\nb"
    return s
end

return z

* Invoke the Module with the following

{{#invoke:Bugs|new_line}}

* The following error will generate

<b>Notice</b>:  unserialize(): Error at offset 75 of 79 bytes...


-----------
Other notes
-----------

* rb / wb did not work

* adding the working fix cited above did work

* The behavior is not specifically related to PHP. I came across this bug in a Java app (XOWA).

The issue appears to be related to how the Lua binary returns "\n" on Windows machines.

* The recursiveEncode in MWServer.lua is clearly measuring a string of length 3:

a:1:{i:1;s:3:"a
b";}

* However, a "\r" somehow shows up in receiveMessage, and needs to be actively removed by the working fix

I'm going to guess that Lua is automatically converting all "\n" to "\r\n" when writing to pipe.read (stream.input) on Windows machines. If so, I think the only "true" fix is to somehow override Lua's default newline behavior on Windows machines. If that's not possible, then the working fix is probably the best alternative.

Hope this helps.
Comment 4 Tim Starling 2013-03-25 01:07:51 UTC
(In reply to comment #3)
> I'm going to guess that Lua is automatically converting all "\n" to "\r\n"
> when writing to pipe.read (stream.input) on Windows machines. If so, I think 
> the only "true" fix is to somehow override Lua's default newline behavior on
> Windows machines. If that's not possible, then the working fix is probably
> the best alternative.

Lua just uses fwrite() on stdout, i.e. the FILE* supplied by the C library called stdout. I don't think there is any way to change what mode it is in, and reopening it would be fairly complex. Note that "\r\n" might also be converted to \n by io.stdin:read() on the Lua side.

Choosing a serialization format that does not use line endings would be one way around it. Converting line endings as appropriate if a Windows Lua binary is detected would be another.
Comment 5 gnosygnu 2013-03-25 03:41:55 UTC
(In reply to comment #4)
> Lua just uses fwrite() on stdout, i.e. the FILE* supplied by the C library
> called stdout. I don't think there is any way to change what mode it is in,
> and
> reopening it would be fairly complex. 

Agreed. I got as far as "io.stdout:write" in "MWServer:sendMessage" and assumed it was Lua. It makes more sense that it is the underlying C library / OS.

> Note that "\r\n" might also be
> converted
> to \n by io.stdin:read() on the Lua side.

I don't think this should be an issue as "encodeLuaVar" in "LuaStandaloneEngine.php" escapes "\r" and "\n" during serialization

  case 'string':
    return '"' .
      strtr( $var, array( 
        '"' => '\\"',
        '\\' => '\\\\',
        "\n" => '\\n',
        "\r" => '\\r',
        "\000" => '\\000',
      ) ) .
      '"';

> Choosing a serialization format that does not use line endings would be one
> way
> around it.

If I understand correctly, are you saying that "\n", "\r" should be escaped on the Lua side (in "recursiveEncode")? If so, I think this is theoretically best, though it may involve a performance hit, which would be especially unnecessary for Linux machines.

> Converting line endings as appropriate if a Windows Lua binary is
> detected would be another.

I personally think this is best, at least for a short-term fix. "LuaStandaloneEngine.php" could detect the OS and apply the working fix if it is Windows. For what it's worth, I used a similar approach in my Java app.
Comment 6 Brad Jorsch 2013-03-28 18:35:18 UTC
Let's not screw around with OS detection, that just seems like a good way to have hard-to-track-down bugs.

Gerrit change #56425 just unconditionally escapes these characters for transport. Windows users, please test.
Comment 7 MWJames 2013-03-28 19:03:58 UTC
After checking out change #56425 and the manually copy of smw.property.lua into the LuaCommon\lualib folder PHPUnit would not crash on windows but return with some meaningful test reports.
Comment 8 Codicorumus 2013-03-28 21:59:54 UTC
I've tested Gerrit change #56425 
(last master revision + two changed code files)
it works for me
(Windows Vista -- Windows NT 6.0)


Tested :
- explicit \n in string
- long-brackets string
- mw.log()


Context :

- in edit-page's "Debug console"
  now OK
  before there was the output
  -----------------------------------------------------
  Lua error: Internal error: Unable to decode message.
  -----------------------------------------------------

- via {{#invoke:...}}
  now OK
  before the returned HTML contained
  -----------------------------------------------------
  <b>Notice</b>:  unserialize(): Error at offset 75 of 79 bytes in <b>[file path]\Scribunto\engines\LuaStandalone\LuaStandaloneEngine.php</b> on line <b>341</b><br>
  -----------------------------------------------------
  [file path] is placeholder for my actual file path
Comment 9 gnosygnu 2013-03-29 06:06:41 UTC
I've also tested Gerrit change #56425.

Specs: Windows XP SP 3, Apache 2.4, PHP 5.4, MediaWiki 1.21wmf11

Confirmed: simple example above now returns a string of "a\nb" (instead of failing).

I've also reviewed the commit diffs and they look fine by me.

Thanks.
Comment 10 Brad Jorsch 2013-04-02 23:38:24 UTC
Merged.
Comment 11 Gerrit Notification Bot 2013-11-14 15:19:28 UTC
Change 95398 had a related patch set uploaded by MarkAHershberger:
(bug 46294) Fix for Windows text-mode file handles

https://gerrit.wikimedia.org/r/95398
Comment 12 Gerrit Notification Bot 2013-11-14 15:40:04 UTC
Change 95398 merged by MarkAHershberger:
(bug 46294) Fix for Windows text-mode file handles

https://gerrit.wikimedia.org/r/95398
Comment 13 Andre Klapper 2013-11-15 11:10:19 UTC
No open patches to review here (backport patch to REL_1_21 got merged), hence resetting status to RESOLVED FIXED. Backport_to_Stable flag might be set to "+" by hexmode.

Note You need to log in before you can comment on or make changes to this bug.


Navigation
Links