From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.7 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 30552 invoked from network); 4 Sep 2020 21:15:21 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 4 Sep 2020 21:15:21 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20200801; t=1599254121; b=qejy8Cl5tB+FdABYnTw8KeTs1gYX9DKjEKa6mJ5Bma36lMSHLOySCXeXBvMR3fAOCJkyqiTk8q ShWpAreLaULq7RHqCmdXAFRJQD35mPSJMFiGdVkOgcjNLD0uZBBv73Cz9VnbMD2XlcF5lEkeDf tJ0mXwOXQePHXp7IEmBwLJgI98TfN2x+D117ripSyvypgpCSmpUFVPeN6kUFi51mmFGmXVq8oS 440NgUlH1LtNpjGdcdP5Cljwm1wWWRrmHs9E5eK/AUk7sNrPJ0zUuk0hFYGGIQTUP3zdU/P37q aW8pwxRlgZzfSaqnhYvZgRPWm2MEPONcJfQDvxH2FoXAsQ==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-ot1-f52.google.com) smtp.remote-ip=209.85.210.52; dkim=pass header.d=brasslantern-com.20150623.gappssmtp.com header.s=20150623 header.a=rsa-sha256; dmarc=none header.from=brasslantern.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20200801; t=1599254121; bh=GefBDpCNOx2eYU+hMfa4dHCmC2h9mlj297f9jw7qkxQ=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Transfer-Encoding:Content-Type:Cc:To:Subject: Message-ID:Date:From:In-Reply-To:References:MIME-Version:DKIM-Signature: DKIM-Signature; b=JeIa8+kDYGxdZt8nprWl/GbafU/I04Xmld+h/PDMM0PeJchTuGiN3dV1qGQl08cbKT9raVQZaX n5PtFGBsDXoVdNm2b4VlgR4p3vr4ye9QxMvkT5ln45DmaB3yDWOhFB68Qrf2aj8Fbt5mtCUiaL r5ZyScZvN5kCQWW4+jgdmy/YXOtzkaq2qq1dla2GqZURW8Q2ixcMQnG7bR0m1I0LmqJ7KPJjjV XKLpjOmuPazdZG2P4X6Wij7E2v7O4V5XXcewDpDQQ7BzoF9ZpoCsUb7iHckBEFJvO1mB2CeZyK V6kXFDx5AMdPBymvvNxa1+sapo9zmbtSShy2fP4L6AoP2Q==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20200801; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Transfer-Encoding: Content-Type:Cc:To:Subject:Message-ID:Date:From:In-Reply-To:References: MIME-Version:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=3svnPav8mpokp3XFzXDnglJM7hfMClnb2O+lkBctC2s=; b=Z6JlVqc19CbqHFA7aDDTK0L1y8 U/ZeTybZ2Ul0nue7LO5HyNKSSmzqDqQXvIR8BjqEFJjzvd1zAZGeLlBBHP5eoOlbVxEPvIDcewHWE cGDmWCjAI2MASDWTnWgjrmen3IwW9ygkWD76TaWXEYSXUW2SNsT9PB3Yf01gd6Rihc8p73wt+ycWX ucG4F7X/YkNTnzSiJ5IyKtxlIJRVAa1q4XVV9Ol7QUF4/FxMjg9CCxuey2ipuJCatI1tR9r5x1M1h CinTra4J8G5cuMN4MfLgXw0VnIkXuIw95HF8/OOCTbKtOmxH5yIE3HQoAeEkIwweDGwwYWwVGKEYF JC4s1juA==; Received: from authenticated user by zero.zsh.org with local id 1kEJ3I-0004TW-Mu; Fri, 04 Sep 2020 21:15:20 +0000 Authentication-Results: zsh.org; iprev=pass (mail-ot1-f52.google.com) smtp.remote-ip=209.85.210.52; dkim=pass header.d=brasslantern-com.20150623.gappssmtp.com header.s=20150623 header.a=rsa-sha256; dmarc=none header.from=brasslantern.com; arc=none Received: from mail-ot1-f52.google.com ([209.85.210.52]:45820) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1kEJ32-0004Jp-IP; Fri, 04 Sep 2020 21:15:05 +0000 Received: by mail-ot1-f52.google.com with SMTP id g96so7105750otb.12 for ; Fri, 04 Sep 2020 14:15:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=3svnPav8mpokp3XFzXDnglJM7hfMClnb2O+lkBctC2s=; b=0b0Lv/vaxDCQIvS6q4PuAFpQwG6sXF1CCQIQb/ZgKYeT/MnryAoHFdRupMfslZdpSm +cCitKYK5AaxM98FLye+EeiOeHOMRgKxK0OYzS1er92hZbyQSjcKsCL8Cyw5A9gh+KC2 ZWW2Jv9XmqXnzZ4CvSR4QUiNURTCJ4YG4JdIMBCf+76QjLSS5flvKCxRKtFz0nsFRIQr 7tEjMWfQhnqNaXEG/xubz+L1J1AdF7cEEMyVwFUdpXoCr9oliuAFL/oWNUYNNpnkEaq9 Tdr0+ogEMH+r+IkBKCbk9seM3SvsYKIi0EwS6GwitmJuzzEIR+O3WjEtDcZrm1LVGmgW xQow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=3svnPav8mpokp3XFzXDnglJM7hfMClnb2O+lkBctC2s=; b=jUJ76/Wj6lwbZlRvXJ7sw4M4/YvhgzRzNoUcADOtcVAOZHlxLBKoAalbGoQbaFLQb8 QP515PIFiKOxrd8qVU0HEwd/tG3zHmx4rzweUnA0Als8p8jC8H90rNdBgnzEAmZw2txh b+gUB7H9i1PMjEVgR9hDaZnYtwu8TFs4tCs4NyCABnvgQ937l1OT3EqEZURfVmf0rpwl lqvYoeyfKZZeHVJe2YBiOlHJIBJvzxgh3WNwfvV+09EgUXgtJzHE3KHn/aPGd1E26yRf s5uTxs6z9FVzTfj0Pzh2lU8VfnZP37xRAhE2IhGXRZgzXmJWnznEx7aGJinbs5B6dJ4q LSQQ== X-Gm-Message-State: AOAM5303jCjnTfXe27YEZrMFN5mfeDjVkJzhIQhigM7eyQgCgXAO3DU/ ysg9EZO8Pe7FCGclS0alHVB81vJPtZZiKb9fG7yIsg== X-Google-Smtp-Source: ABdhPJzo4LAWIYKMpyTwWtBQ/TApsf7uvFtLxWK/jHjuV+Api7MZwK74LrIZfiy2EfQM6VqGkWwgatJyzrkdReHLpUM= X-Received: by 2002:a9d:ae8:: with SMTP id 95mr7174513otq.260.1599254101740; Fri, 04 Sep 2020 14:15:01 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Bart Schaefer Date: Fri, 4 Sep 2020 14:14:50 -0700 Message-ID: Subject: Re: Is there a way to put a timeout on a function? To: Rudi C Cc: "zsh-workers@zsh.org" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Seq: 47361 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: Archived-At: On Mon, Aug 31, 2020 at 8:00 AM Rudi C wr= ote: > > I need to limit the execution time of arbitrary zsh functions. Is there a= way to achieve this? I'm ok with just adding a time limit to all the exter= nal commands, but I do not know how to do that. (I don't want to manually p= refix all my commands with the GNU timeout command.) Here's a basic outline; you'll have to fiddle with it to get the details as you want: # This is the default, but for clarity setopt DEBUG_BEFORE_CMD TRAPDEBUG() { # Prevent the command from running twice setopt localoptions ERR_EXIT # Set up the timeout sleep $DESIRED_TIMEOUT & timer=3D$! # Background so parent can wait for timer (){ eval "${ZSH_DEBUG_CMD}"; kill $timer } & victim=3D$! wait $timer && kill $victim } Note that this prevents anything from changing the state of the current shell, so be careful experimenting with it. One of the things you probably need to "fiddle with" is to have this function return BEFORE it sets ERR_EXIT for any command that SHOULD run in the current shell. Another is that this buries the exit status of the command, so you need a way to capture it. > On a sidenote, I would also like to make the shell stop forcefully (like = when a glob fails) if it doesn't find an external command. I thought maybe = these two problems might have similar solutions. Is prexec viable for them?= Does preexec even work for noninteractive scripts? Preexec and precmd are strictly for interactive shells. Another potentially dangerous solution: command_not_found_handler() { builtin print "command not found: $*" builtin kill -USR1 $$ } TRAPUSR1() { exit 1 } Aside to -workers: I've had this go into an infinite recursion if the TRAPUSR1 definition precedes the command_not_found_handler definition and/or the "builtin" prefix is not used. This seems odd, but is not reliably reproducible.