Exception when calling reply_json/1

69 views
Skip to first unread message

vladac...@gmail.com

unread,
May 15, 2014, 12:45:56 PM5/15/14
to swi-p...@googlegroups.com
Hi,

I am getting the following error message when calling the reply_json/1 predicate:


Foreign predicate http_stream:cgi_discard/1 did not clear exception: error(io_error(write,<stream>(02CB4060)),context(http_stream:cgi_discard/1,No error))


I have done some testing to try and figure out what is causing it, and have determined that it is a combination of calling another predicate defined by me (xmi2pl/1) before calling reply_json_1 and using the HTTP libraries. Namely, the following code causes the exception to appear:


:- use_module(library(http/http_json)).
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module('.././xmi2pl/xmi2pl.pl').

:- http_handler('/json_test', json_test, []).

test(Port) :- http_server(http_dispatch, [port(Port)]).

json_test(_) :-
    xmi2pl("1"),
    reply_json(json([id="1"])).



However, the following code DOES NOT cause the exception to appear:


:- use_module(library(http/http_json)).
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module('.././xmi2pl/xmi2pl.pl').

:- http_handler('/json_test', json_test, []).

test(Port) :- http_server(http_dispatch, [port(Port)]).

json_test(_) :- reply_json(json([id="1"])).



Third, the following code DOES NOT cause the exception to appear:


:- use_module(library(http/http_json)).
:- use_module('.././xmi2pl/xmi2pl.pl').

json_test() :-
    xmi2pl("1"),
    reply_json(json([id="1"])).


What could I be doing inside the xmi2pl/1 predicate which can cause reply_json/1 to fail? My first guess was that I am setting some global flag/setting that indirectly affects reply_json/1, but I can't find something like that in my code.

Best regards,
Vlad

Jan Wielemaker

unread,
May 16, 2014, 3:49:34 AM5/16/14
to vladac...@gmail.com, swi-p...@googlegroups.com
You don't give us much clue as to what xmi2pl/1 does. I don't think
there are any flags involved. What is critical though is that you
cannot write to current_output and you must make sure that you do
not mess around with this stream. Within the handler, current_output
is bound to a special wrapped stream that handles the replied CGI
header, encoding, chunking, etc.

Hope that gives you a hint.

Cheers --- Jan

vladac...@gmail.com

unread,
May 16, 2014, 6:05:29 AM5/16/14
to swi-p...@googlegroups.com
Hi Jan,

Thank you for the suggestion. I found some calls to write/1 in my code and commented them out. This seems to have fixed the problem. The purpose of xmi2pl/1 is to convert an XMI file (XML-based format used for storing UML models) into a Prolog fact database.

Best regards,
Vlad
Reply all
Reply to author
Forward
0 new messages