<div dir="ltr">My bet is on a framing problem. Joe indirectly gives you a way to handle the question: Test the binary_to_term locally, without the socket. Or send something you know over the wire and check it isn't garbled. This eliminates eventual zlib trouble.<div><br></div><div>The framing problem is subtle: The kernel could either deliver 1 byte a time from the stream, which is inefficient, or it could wait for eternity and just buffer everything never delivering, which is wrong. In practice, the kernel chooses some middle ground. It will deliver bytes from the stream to you either because it has a full buffer internally, or because some time has passed since the last delivery, or that your application is usually going to ask for more data rather quickly. The MTU of your IP datagrams can quickly come into play and you will get messages roughly of MTU size or a multiple hereof. The internet can do evil things to your packets however: fragment them, reorder them, omit a packet in the TCP sequence which requires a SACK roundtrip and so on. These events affects the way the kernel sends bytes to the application in the stream. </div><div><br></div><div>Since it is a stream, you need your code to handle this case (untested):</div><div><br></div><div>read_packet(Pkt, Buffer) -></div><div> read_buffer(<<Buffer/binary, Pkt/binary>>).</div><div><br></div><div>read_buffer(<<L:32/integer, Payload:L/binary, RestBuffer/binary>>) -></div><div> {packet, Payload, RestBuffer};</div><div>read_buffer(Buffer) -></div><div> {need_more_data, Buffer}.</div><div><br></div><div>write_term(Sock, Term) -></div><div> Payload = term_to_term(Term),</div><div> L = byte_size(Payload),</div><div> gen_tcp:write(Sock, [<<L:32/integer>>, Payload]).</div><div><br></div><div>Coincidentally, this is roughly what the option {packet, 4} does at the VM layer and it is quicker. In the case where we lack enough bytes in the buffer from the kernel, we can tell the world we need more data. If we can break off a packet from the buffer, we do that and return what is remaining in the buffer. The above API requires the caller to repeatedly call read_buffer/1 on the buffer until we can't squeeze more packets out of the buffer. Another variant would just return a list of decoded packets and a buffer state for the things which weren't able to be decoded.</div></div><br><div class="gmail_quote"><div dir="ltr">On Sun, Jan 29, 2017 at 12:56 PM Ali Sabil <<a href="mailto:ali.sabil@gmail.com">ali.sabil@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">You don't seem to be using any framing over TCP. TCP is stream based not message based.<br class="gmail_msg"><br class="gmail_msg">Basically what happens is that your message when it gets too large it gets split and sent in multiple packets. You need to add framing around your data to ensure that the entire message is received before you attempt a call to binary_to_term.<br class="gmail_msg"><div class="gmail_quote gmail_msg"><div dir="ltr" class="gmail_msg">On Sun, 29 Jan 2017 at 11:50, Joe Armstrong <<a href="mailto:erlang@gmail.com" class="gmail_msg" target="_blank">erlang@gmail.com</a>> wrote:<br class="gmail_msg"></div><blockquote class="gmail_quote gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">No idea what's wrong - but you could start checking that<br class="gmail_msg">
<br class="gmail_msg">
1) binary_to_term works locally (it should)<br class="gmail_msg">
2) the data is transmitted correctly<br class="gmail_msg">
print the md5 checksum of the binary before sending and after reception<br class="gmail_msg">
erlang has a BIF for this erlang:md5<br class="gmail_msg">
<br class="gmail_msg">
/Joe<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
On Sat, Jan 28, 2017 at 1:55 AM, Kevin Johnson<br class="gmail_msg">
<<a href="mailto:johnson786.kevin@gmail.com" class="gmail_msg" target="_blank">johnson786.kevin@gmail.com</a>> wrote:<br class="gmail_msg">
> Hi,<br class="gmail_msg">
><br class="gmail_msg">
> I am using Elixir to send data using :gen_tcp. The syntax presented here may<br class="gmail_msg">
> be elementary Elixir syntax, however the issue is specifically related to<br class="gmail_msg">
> the usage of term_to_binary and binary_to_term conversions over gen_tcp.<br class="gmail_msg">
><br class="gmail_msg">
> This is the command I used on the client side:<br class="gmail_msg">
>><br class="gmail_msg">
>> :gen_tcp.send(client_socket, :erlang.term_to_binary(data))<br class="gmail_msg">
><br class="gmail_msg">
><br class="gmail_msg">
> This is the command I use on the server side:<br class="gmail_msg">
>><br class="gmail_msg">
>> defp handle_data(data) do<br class="gmail_msg">
>> data<br class="gmail_msg">
>> |> :erlang.binary_to_term<br class="gmail_msg">
>> end<br class="gmail_msg">
><br class="gmail_msg">
><br class="gmail_msg">
> When my data gets received on the server side, it seems that at a certain<br class="gmail_msg">
> size threshold that it errors out like this:<br class="gmail_msg">
><br class="gmail_msg">
>> pry(6)> [error] GenServer #PID<0.502.0> terminating<br class="gmail_msg">
>><br class="gmail_msg">
>> ** (ArgumentError) argument error<br class="gmail_msg">
>><br class="gmail_msg">
>> :erlang.binary_to_term(<<131, 116, 0, 0, 0, 28, 109, 0, 0, 0, 5, 66, 49,<br class="gmail_msg">
>> 95, 81, 49, 108, 0, 0, 0, 1, 116, 0, 0, 0, 1, 109, 0, 0, 0, 4, 116, 101,<br class="gmail_msg">
>> 120, 116, 109, 0, 0, 0, 11, 84, 69, 88, 84, 32, 65, 78, 83, 87, 69, ...>>)<br class="gmail_msg">
>><br class="gmail_msg">
>> (stdlib) gen_server.erl:601: :gen_server.try_dispatch/4<br class="gmail_msg">
>><br class="gmail_msg">
>> (stdlib) gen_server.erl:667: :gen_server.handle_msg/5<br class="gmail_msg">
>><br class="gmail_msg">
>> (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3<br class="gmail_msg">
>><br class="gmail_msg">
>> Last message: {:tcp, #Port<0.10115>, <<131, 116, 0, 0, 0, 28, 109, 0, 0,<br class="gmail_msg">
>> 0, 5, 66, 49, 95, 81, 49, 108, 0, 0, 0, 1, 116, 0, 0, 0, 1, 109, 0, 0, 0, 4,<br class="gmail_msg">
>> 116, 101, 120, 116, 109, 0, 0, 0, 11, 84, 69, 88, 84, 32, 65, 78, 83, ...>>}<br class="gmail_msg">
><br class="gmail_msg">
><br class="gmail_msg">
> Here is some example data that will just pass fine:<br class="gmail_msg">
> %{"B2_Q18" => [%{"choice_id" => "B2_Q18_C1"}], "B3_B9_Q1" => [%{"choice_id"<br class="gmail_msg">
> => "B3_B9_Q1_C1"}], "B2_Q7" => [%{"choice_id" => "B2_Q7_C1"}], "B5_B1_Q8" =><br class="gmail_msg">
> [%{"choice_id" => "B5_B1_Q8_C1"}], "B3_B6_Q1" => [%{"choice_id" =><br class="gmail_msg">
> "B3_B6_Q1_C1"}], "B5_B2_Q5" => [%{"choice_id" => "B5_B2_Q5_C1"},<br class="gmail_msg">
> %{"choice_id" => "B5_B2_Q5_C2"}], "B3_B7_Q1" => [%{"choice_id" =><br class="gmail_msg">
> "B3_B7_Q1_C1"}], "B3_B4_Q1" => [%{"choice_id" => "B3_B4_Q1_C1"}], "B2_Q16"<br class="gmail_msg">
> => [%{"choice_id" => "B2_Q16_C1"}], "B2_Q20" => [%{"choice_id" =><br class="gmail_msg">
> "B2_Q20_C1"}], "B2_Q5" => [%{"choice_id" => "B2_Q5_C1"}], "B2_Q9" =><br class="gmail_msg">
> [%{"choice_id" => "B2_Q9_C1"}], "B2_Q17" => [%{"choice_id" => "B2_Q17_C1"}],<br class="gmail_msg">
> "B3_B2_Q1" => [%{"choice_id" => "B3_B2_Q1_C1"}], "B5_B2_Q3" =><br class="gmail_msg">
> [%{"choice_id" => "B5_B2_Q3_C1"}], "B3_B3_Q1" => [%{"choice_id" =><br class="gmail_msg">
> "B3_B3_Q1_C1"}], "B5_B1_Q1" => [%{"text" => "TEXT ANSWER"}], "B2_Q14" =><br class="gmail_msg">
> [%{"choice_id" => "B2_Q14_C1"}], "B5_B1_Q3" => [%{"choice_id" =><br class="gmail_msg">
> "B5_B1_Q3_C22"}], "B5_B2_Q2" => [%{"choice_id" => "B5_B2_Q2_C1"}], "B2_Q4"<br class="gmail_msg">
> => [%{"choice_id" => "B2_Q4_C1"}], "B2_Q10" => [%{"choice_id" =><br class="gmail_msg">
> "B2_Q10_C1"}], "B4_Q1" => [%{"choice_id" => "B4_Q1_C1"}, %{"choice_id" =><br class="gmail_msg">
> "B4_Q1_C10"}], "B5_B1_Q6" => [%{"choice_id" => "B5_B1_Q6_C1"}], "B2_Q6" =><br class="gmail_msg">
> [%{"choice_id" => "B2_Q6_C1"}], "B2_Q2" => [%{"choice_id" => "B2_Q2_C1"}],<br class="gmail_msg">
> "B2_Q1" => [%{"choice_id" => "B2_Q1_C1"}]}<br class="gmail_msg">
><br class="gmail_msg">
> The moment I make it slightly longer, by like appending an extra key "a" =><br class="gmail_msg">
> "b" it will give me the above error.<br class="gmail_msg">
><br class="gmail_msg">
> It seems to be that the size of the keys plays an issue here, because if I<br class="gmail_msg">
> convert all the above "choice_id" keys to a mere "c", then even with the<br class="gmail_msg">
> extra entry "a" => "b" included like following below will work just fine:<br class="gmail_msg">
> %{"B2_Q18" => [%{"c" => "B2_Q18_C1"}], "B3_B9_Q1" => [%{"c" =><br class="gmail_msg">
> "B3_B9_Q1_C1"}], "B2_Q7" => [%{"c" => "B2_Q7_C1"}], "B5_B1_Q8" => [%{"c" =><br class="gmail_msg">
> "B5_B1_Q8_C1"}], "B3_B6_Q1" => [%{"c" => "B3_B6_Q1_C1"}], "B5_B2_Q5" =><br class="gmail_msg">
> [%{"c" => "B5_B2_Q5_C1"}, %{"c" => "B5_B2_Q5_C2"}], "B3_B7_Q1" => [%{"c" =><br class="gmail_msg">
> "B3_B7_Q1_C1"}], "B3_B4_Q1" => [%{"c" => "B3_B4_Q1_C1"}], "B2_Q16" => [%{"c"<br class="gmail_msg">
> => "B2_Q16_C1"}], "B2_Q20" => [%{"c" => "B2_Q20_C1"}], "B2_Q5" => [%{"c" =><br class="gmail_msg">
> "B2_Q5_C1"}], "B2_Q9" => [%{"c" => "B2_Q9_C1"}], "B2_Q17" => [%{"c" =><br class="gmail_msg">
> "B2_Q17_C1"}], "B3_B2_Q1" => [%{"c" => "B3_B2_Q1_C1"}], "B5_B2_Q3" => [%{"c"<br class="gmail_msg">
> => "B5_B2_Q3_C1"}], "B3_B3_Q1" => [%{"c" => "B3_B3_Q1_C1"}], "B5_B1_Q1" =><br class="gmail_msg">
> [%{"text" => "TEXT ANSWER"}], "B2_Q14" => [%{"c" => "B2_Q14_C1"}],<br class="gmail_msg">
> "B5_B1_Q3" => [%{"c" => "B5_B1_Q3_C22"}], "B5_B2_Q2" => [%{"c" =><br class="gmail_msg">
> "B5_B2_Q2_C1"}], "B2_Q4" => [%{"c" => "B2_Q4_C1"}], "B2_Q10" => [%{"c" =><br class="gmail_msg">
> "B2_Q10_C1"}], "B4_Q1" => [%{"c" => "B4_Q1_C1"}, %{"c" => "B4_Q1_C10"}],<br class="gmail_msg">
> "B5_B1_Q6" => [%{"c" => "B5_B1_Q6_C1"}], "B2_Q6" => [%{"c" => "B2_Q6_C1"}],<br class="gmail_msg">
> "B2_Q2" => [%{"c" => "B2_Q2_C1"}], "B2_Q1" => [%{"c" => "B2_Q1_C1"}], "a" =><br class="gmail_msg">
> "b"}<br class="gmail_msg">
><br class="gmail_msg">
> The following on the other hand will just fail:<br class="gmail_msg">
> data =<br class="gmail_msg">
> %{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc<br class="gmail_msg">
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"<br class="gmail_msg">
> => "q"}<br class="gmail_msg">
> :gen_tcp.send(client_socket, :erlang.term_to_binary(data))<br class="gmail_msg">
><br class="gmail_msg">
> The only issue that I was able to track down here is a conjunction of two<br class="gmail_msg">
> things:<br class="gmail_msg">
> 1) A limit to the size of total key length inside a map/hash<br class="gmail_msg">
> 2) sending that hash as a binary over tcp and converting back to term<br class="gmail_msg">
><br class="gmail_msg">
> In all of the above cases, a direct conversion of :erlang.term_to_binary<br class="gmail_msg">
> followed by :erlang.binary_to_term works just fine without any issues. The<br class="gmail_msg">
> issue only comes about after that binary was send over tcp and then<br class="gmail_msg">
> :erlang.binary_to_term is attempted.<br class="gmail_msg">
><br class="gmail_msg">
> I would greatly appreciate if anyone can provide me guidance in this matter,<br class="gmail_msg">
> point out to me any relevant documentation that officially states any of<br class="gmail_msg">
> these constraints if they are in fact constraints, and if they are not<br class="gmail_msg">
> supposed to be constraints then kindly instruct me what additional<br class="gmail_msg">
> information may be needed for me to get to the bottom of this.<br class="gmail_msg">
><br class="gmail_msg">
> Thank you.<br class="gmail_msg">
><br class="gmail_msg">
> _______________________________________________<br class="gmail_msg">
> erlang-questions mailing list<br class="gmail_msg">
> <a href="mailto:erlang-questions@erlang.org" class="gmail_msg" target="_blank">erlang-questions@erlang.org</a><br class="gmail_msg">
> <a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" class="gmail_msg" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br class="gmail_msg">
><br class="gmail_msg">
_______________________________________________<br class="gmail_msg">
erlang-questions mailing list<br class="gmail_msg">
<a href="mailto:erlang-questions@erlang.org" class="gmail_msg" target="_blank">erlang-questions@erlang.org</a><br class="gmail_msg">
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" class="gmail_msg" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br class="gmail_msg">
</blockquote></div>
_______________________________________________<br class="gmail_msg">
erlang-questions mailing list<br class="gmail_msg">
<a href="mailto:erlang-questions@erlang.org" class="gmail_msg" target="_blank">erlang-questions@erlang.org</a><br class="gmail_msg">
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" class="gmail_msg" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br class="gmail_msg">
</blockquote></div>