From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Chris Hunt Newsgroups: gmane.emacs.help Subject: Why does calling server-eval-at seem to change the behavior of read-string later? Date: Wed, 11 Nov 2020 18:15:08 -0500 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="3002"; mail-complaints-to="usenet@ciao.gmane.io" To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Thu Nov 12 03:34:11 2020 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kd2R5-0000YM-S0 for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 12 Nov 2020 03:34:07 +0100 Original-Received: from localhost ([::1]:55578 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kd2R4-00045L-U3 for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 11 Nov 2020 21:34:06 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:35798) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kczLF-0000hJ-8z for help-gnu-emacs@gnu.org; Wed, 11 Nov 2020 18:15:53 -0500 Original-Received: from mail-ot1-x330.google.com ([2607:f8b0:4864:20::330]:44122) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kczL9-0000js-G1 for help-gnu-emacs@gnu.org; Wed, 11 Nov 2020 18:15:49 -0500 Original-Received: by mail-ot1-x330.google.com with SMTP id f16so3783648otl.11 for ; Wed, 11 Nov 2020 15:15:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=RVkrfI0nI7+adYxdooibPFN8CPxbFDy5DcIU7IAXkSc=; b=fxAcPlWHjUz/ghOyJYcziPcUBZFpAI/+qQ3bxntdbZfp80fU2+BoKiFtgffpnigO+S ztNVbQJZYWWC77adUFy8gB+a+b/qe8NK3+uYi2J7Af3FDw+GBrGqJGjnp1Cokt6ZYLjU udlJ/UvNC0MjGASLlPkTdokqkypKh59MgJMrJOHka4XbFp7pRVQHaAwoa687ZxOIcnvK X+78Xlwvqfry6ui9t4MBcDnrMYwn1UK8zzzSAFgL6LTJYBjAskopXMzZB8X1Gc8nM2HZ lCqwtHlUE6MiHYY9Y4rRBhW+MC4O8WQaRpucaTSI50TnhBR0Qx9siGMxO1J6EfCDs1SI P9FA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=RVkrfI0nI7+adYxdooibPFN8CPxbFDy5DcIU7IAXkSc=; b=mieGk/a1YrZAhf8ILxs9KVSeFfVvlr7CMXEzNWa+WQDbqIMDE6hJ0xPLKaMhv9viar Ku2/jHR8VIaODJ3rXg1ml2tiYxnjdzjqAVOUkGz5bD5p+t/ECPTt0H+/YJftsRRw7BkV m8Ccbm4kbD0CU2vlGH0Bh7duosETOjfDjBvvYSHlhqxY9IF+NOxqR3CfdjlAZSRZ/QEj OeBF13st/mxd4ERfU4+mqEAK6MjS+rOE2xJShz4DQhGfR2bC8K/5QN/h3XPwn8YQZ3wF SzwSR0w0LN2WaQ+FfOStb7Apgi5acympFzfHs5Fo+wN2rGdm6J2QLdXOjqJG7m77BPFF Xh5g== X-Gm-Message-State: AOAM530egYsdd34GOcRiS8E3CCY7IgCOEw2kwyQ1WynfD55X7HmkKU/2 /2HLslfGLsiAMVznbZSfNfElHXdNoLtdSrx/nRLK0AvkQpfoYg== X-Google-Smtp-Source: ABdhPJy3QAOCGZYGGad+qr+6chEwnXV0RP5bbzj68tCs06vcbigLuTeesHwJ9TvzxczIhVIxyKYuSgYUMwvxEx29Q1A= X-Received: by 2002:a05:6830:1bd7:: with SMTP id v23mr12235897ota.12.1605136544121; Wed, 11 Nov 2020 15:15:44 -0800 (PST) Received-SPF: pass client-ip=2607:f8b0:4864:20::330; envelope-from=chrahunt@gmail.com; helo=mail-ot1-x330.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Wed, 11 Nov 2020 21:33:47 -0500 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:125246 Archived-At: Hello, This is cross-posted from https://emacs.stackexchange.com/q/61676/29147, which hasn't received much feedback. I am trying to create a script that, when run with emacs, will read commands from stdin a line at a time, execute them in another emacs process (with `server-eval-at`) and print the result. The process should exit when no more input is available. I have come up with a basic protocol and everything seems to work when using `eval` locally, but after introducing `server-eval-at` the emacs process hangs instead of exiting at the end of input. For initial development I was doing the evaluation inline, in the same process that was reading the input. That seems to work well. That script looks like (while (setq command (ignore-errors (read-string ""))) (setq command (read command)) (message "[DEBUG] Received command %s" command) (let ((result (eval command))) (message "[DEBUG] Response received %s" result))) In a shell script, for testing: #!/bin/sh cd "$(mktemp -d)" cat < script.el (while (setq command (ignore-errors (read-string ""))) (setq command (read command)) (message "[DEBUG] Received command %s" command) (let ((result (eval command))) (message "[DEBUG] Response received %s" result))) EOF echo '(message "hello world")' | \ emacs --quick --batch --script "$PWD/script.el" An example of running this, with output: $ ./t2 [DEBUG] Received command (message hello world) hello world [DEBUG] Response received hello world $ As you can see, the debug messages are printed. The process exits at the end of input. All of this is expected and OK. Now I want to run the incoming commands on the server, so I use `server-eval-at`. Here is that script (require 'server) (while (setq command (ignore-errors (read-string ""))) (setq command (read command)) (message "[DEBUG] Received command %s" command) (let ((result (server-eval-at (getenv "RPC_SERVER_NAME") command))) (message "[DEBUG] Response received %s" result))) Another shell script, for testing #!/bin/sh cd "$(mktemp -d)" export RPC_SERVER_NAME="$PWD/emacs" emacs --quick "--fg-daemon=$RPC_SERVER_NAME" >stdout.log 2>stderr.log & sleep 1 cat < script.el (require 'server) (while (setq command (ignore-errors (read-string ""))) (setq command (read command)) (message "[DEBUG] Received command %s" command) (let ((result (server-eval-at (getenv "RPC_SERVER_NAME") command))) (message "[DEBUG] Response received %s" result))) EOF echo '(message "hello world")' | \ emacs --quick --batch --script "$PWD/script.el" An example of running this, with output: $ ./t1 [DEBUG] Received command (message hello world) [DEBUG] Response received hello world The output looks fine, but the process does not exit! I have inspected these processes a few ways: 1. top shows that the `server-eval-at` emacs (client) is consuming 100% CPU. 2. strace shows that the `server-eval-at` emacs (client) is calling `read` on stdin (fd 0) repeatedly, and receiving an empty string and 0 result each time 3. strace shows that the `eval` emacs calls `read` on stdin twice: once to read the command, and once receiving an empty string and 0 result before exiting. 4. gdb on the `server-eval-at` emacs (client) has a Lisp Backtrace with `read-string` at the top. I inlined `server-eval-at` and was able to identify that calling [`accept-process-output`](https://github.com/emacs-mirror/emacs/blob/79d04ae13ff33a93f631061d912168e9703251dd/lisp/server.el#L1739) is necessary for the subsequent call to `read-string` (in the next loop iteration) to behave differently, but it is hard for me to dig much deeper than that. I tried these same steps against emacs 26.3 and observed the same result. I am using emacs 27.1 on Ubuntu Linux 18.04, as installed with evm. Any help is appreciated! Regards, Chris