phrase_from_stream/2 nontermination (Stream from http_open/3) #2437
-
I'm building an application that has a HTTP Client component. It would be nice to streaming DCG to parse the data but phrase_from_stream/2 does not terminate when using the instantiated stream from http_open/3. Steps to reproduce in top-level scryer-prolog: (Started a simple local HTTP server using ?- use_module(library(charsio)).
true.
?- use_module(library(dcgs)).
true.
?- use_module(library(http/http_open)).
true.
% reading the entire stream before parsing works as expected:
?- http_open("http://127.0.0.1:8000", Stream, []), get_n_chars(Stream, N, Chars), phrase(seq(Data), Chars).
Stream = '$stream'(0x600000636908), N = 726, Chars = "<!DOCTYPE HTML>\n<ht ...", Data = "<!DOCTYPE HTML>\n<ht ...".
?- use_module(library(pio)).
true.
% phrase_from_stream/2 does not terminate in this case:
?- http_open("http://127.0.0.1:8000", Stream, []), phrase_from_stream(seq(Data), Stream).
% nontermination This seems to happen with other grammar rules as well (I'm originally trying to parse JSON data), but I'm able to reproduce with any rule. I also attempted to unify with content size and set Similar code using SWI-Prolog works as expected: ?- use_module(library(http/http_open)).
true.
?- use_module(library(dcg/basics)).
true.
?- use_module(library(pio)).
true.
?- http_open("http://127.0.0.1:8000", Stream, []), phrase_from_stream(string(Data), Stream).
Stream = <stream>(0x600002954400,0x600002974b00),
Data = [60, 33, 68, 79, 67, 84, 89, 80, 69|...] ;
false. I was able to isolate the issue further. it looks like Scryer: ?- http_open("http://127.0.0.1:8000", S, []), get_n_chars(S, N, Chars), at_end_of_stream(S).
false.
?- http_open("http://127.0.0.1:8000", S, []), get_n_chars(S, N, Chars), stream_property(S, end_of_stream(X)).
S = '$stream'(0x600002108888), N = 724, Chars = "<!DOCTYPE HTML>\n<ht ...", X = not. SWI: ?- http_open("http://127.0.0.1:8000", Stream, []), read_string(Stream, N, String), at_end_of_stream(Stream).
Stream = <stream>(0x600002b38900,0x600002b38300),
N = 724,
String = "<!DOCTYPE HTML>..." I debugged the library pio, and it looks like it gets stuck into a infinite loop in the internal (tail) recursive |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 5 replies
-
I think, it's a bug. And I've encountered some crashes, for example this I can easily reproduce:
And another example of "abnormal termination" using Ctrl-D:
And even without using Ctrl+D, I can "compose" them:
|
Beta Was this translation helpful? Give feedback.
-
Non-termination: 7x
|
Beta Was this translation helpful? Give feedback.
-
Again, maybe not directly related, and again a "Ctrl-D example": (from my naive point of view) unexpected
And using Ctrl+C:
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
The implementation of a method in the streams code was missing, as a result it always reported that there were more bytes to read even if it wasn't the case. It's fixed here: #2442 |
Beta Was this translation helpful? Give feedback.
-
@aarroyoc Thank you for your knowledge, skills, talent, time and work, thank you very much for fixing the phrase_from_stream/2 non-termination in this context of http_open/3. I don't want to write more here, but I think, I have to add a margin note, that my (not directly related to the use case) "bad examples" continue working, as a separate issue. |
Beta Was this translation helpful? Give feedback.
The implementation of a method in the streams code was missing, as a result it always reported that there were more bytes to read even if it wasn't the case. It's fixed here: #2442