diff --git a/5pkgs/jesec-rtorrent/avoid-stack-overflow-for-lockfile-buf.patch b/5pkgs/jesec-rtorrent/avoid-stack-overflow-for-lockfile-buf.patch
new file mode 100644
index 0000000..2a20b15
--- /dev/null
+++ b/5pkgs/jesec-rtorrent/avoid-stack-overflow-for-lockfile-buf.patch
@@ -0,0 +1,30 @@
+From dd4a96073d4a60ca8fff55be6ea6b17018de96a8 Mon Sep 17 00:00:00 2001
+From: Varun Madiath <git@madiathv.com>
+Date: Wed, 19 Jul 2023 15:30:57 -0400
+Subject: [PATCH] utils: lockfile: avoid stack overflow for lockfile buffer
+
+Original patch by @cyphar was submitted to rakshasa/rtorrent at
+https://github.com/rakshasa/rtorrent/pull/1169.
+
+Observed the segfault on nixos-unstable.
+---
+ src/utils/lockfile.cc | 3 ++-
+ 1 file changed, 2 insertions(+), 1 deletion(-)
+
+diff --git a/src/utils/lockfile.cc b/src/utils/lockfile.cc
+index 76e4b8f..441f5c8 100644
+--- a/src/utils/lockfile.cc
++++ b/src/utils/lockfile.cc
+@@ -75,7 +75,8 @@ Lockfile::try_lock() {
+   int  pos = ::gethostname(buf, 255);
+ 
+   if (pos == 0) {
+-    ::snprintf(buf + std::strlen(buf), 255, ":+%i\n", ::getpid());
++    ssize_t len = std::strlen(buf);
++    ::snprintf(buf + len, 255 - len, ":+%i\n", ::getpid());
+     ssize_t __attribute__((unused)) result = ::write(fd, buf, std::strlen(buf));
+   }
+ 
+-- 
+2.41.0
+
diff --git a/5pkgs/jesec-rtorrent/default.nix b/5pkgs/jesec-rtorrent/default.nix
index 9130760..883d995 100644
--- a/5pkgs/jesec-rtorrent/default.nix
+++ b/5pkgs/jesec-rtorrent/default.nix
@@ -1,31 +1,30 @@
-{
-  lib,
-  callPackage,
-  stdenv,
-  fetchFromGitHub,
-  cmake,
-  curl,
-  gtest,
-  ncurses,
-  jsonRpcSupport ? true,
-  nlohmann_json,
-  xmlRpcSupport ? true,
-  xmlrpc_c,
+{ lib
+, stdenv
+, fetchFromGitHub
+, cmake
+, curl
+, gtest
+, libtorrent
+, ncurses
+, jsonRpcSupport ? true, nlohmann_json
+, xmlRpcSupport ? true, xmlrpc_c
 }:
-let
-  libtorrent = callPackage ./libtorrent.nix {};
-in
+
 stdenv.mkDerivation rec {
   pname = "jesec-rtorrent";
-  version = "0.9.8-r16-unstable-2023-07-21";
+  version = "0.9.8-r16";
 
   src = fetchFromGitHub {
     owner = "jesec";
     repo = "rtorrent";
-    rev = "199e8f85244c8eb1c30163d51755570ad86139bb";
-    hash = "sha256-AWWOvvUNNOIbNiwY/uz55iKt8A0YuMsyWGjaLgKUOCY=";
+    rev = "v${version}";
+    hash = "sha256-i7c1jSawHshj1kaXl8tdpelIKU24okeg9K5/+ht6t2k=";
   };
 
+  patches = [
+    ./avoid-stack-overflow-for-lockfile-buf.patch
+  ];
+
   passthru = {
     inherit libtorrent;
   };
@@ -34,21 +33,19 @@ stdenv.mkDerivation rec {
     cmake
   ];
 
-  buildInputs =
-    [
-      curl
-      libtorrent
-      ncurses
-    ]
-    ++ lib.optional jsonRpcSupport nlohmann_json
-    ++ lib.optional xmlRpcSupport xmlrpc_c;
+  buildInputs = [
+    curl
+    libtorrent
+    ncurses
+  ]
+  ++ lib.optional jsonRpcSupport nlohmann_json
+  ++ lib.optional xmlRpcSupport xmlrpc_c;
 
-  cmakeFlags =
-    [
-      "-DUSE_RUNTIME_CA_DETECTION=NO"
-    ]
-    ++ lib.optional (!jsonRpcSupport) "-DUSE_JSONRPC=NO"
-    ++ lib.optional (!xmlRpcSupport) "-DUSE_XMLRPC=NO";
+  cmakeFlags = [
+    "-DUSE_RUNTIME_CA_DETECTION=NO"
+  ]
+  ++ lib.optional (!jsonRpcSupport) "-DUSE_JSONRPC=NO"
+  ++ lib.optional (!xmlRpcSupport) "-DUSE_XMLRPC=NO";
 
   doCheck = true;
 
diff --git a/5pkgs/jesec-rtorrent/ed30d713e06dbe14edffd3afdf465d67ad69174f.patch b/5pkgs/jesec-rtorrent/ed30d713e06dbe14edffd3afdf465d67ad69174f.patch
new file mode 100644
index 0000000..4a2478a
--- /dev/null
+++ b/5pkgs/jesec-rtorrent/ed30d713e06dbe14edffd3afdf465d67ad69174f.patch
@@ -0,0 +1,78 @@
+From ed30d713e06dbe14edffd3afdf465d67ad69174f Mon Sep 17 00:00:00 2001
+From: Jesse Chan <jc@linux.com>
+Date: Sat, 14 May 2022 16:21:31 -0700
+Subject: [PATCH] include: include what you use
+
+---
+ include/protocol/handshake.h         | 1 +
+ include/protocol/protocol_base.h     | 2 ++
+ include/test/helpers/mock_compare.h  | 3 +++
+ include/torrent/object_raw_bencode.h | 1 +
+ include/torrent/utils/allocators.h   | 1 +
+ 5 files changed, 8 insertions(+)
+
+diff --git a/include/protocol/handshake.h b/include/protocol/handshake.h
+index 8ce8268d..bc654f0a 100644
+--- a/include/protocol/handshake.h
++++ b/include/protocol/handshake.h
+@@ -7,6 +7,7 @@
+ #include "handshake_encryption.h"
+ #include "net/protocol_buffer.h"
+ #include "net/socket_stream.h"
++#include "net/throttle_list.h"
+ #include "torrent/bitfield.h"
+ #include "torrent/peer/peer_info.h"
+ #include "torrent/utils/priority_queue_default.h"
+diff --git a/include/protocol/protocol_base.h b/include/protocol/protocol_base.h
+index 18229b0b..0f23fdd4 100644
+--- a/include/protocol/protocol_base.h
++++ b/include/protocol/protocol_base.h
+@@ -5,6 +5,8 @@
+ #define LIBTORRENT_NET_PROTOCOL_BASE_H
+ 
+ #include "net/protocol_buffer.h"
++#include "net/throttle_list.h"
++#include "torrent/data/piece.h"
+ #include "torrent/utils/timer.h"
+ 
+ namespace torrent {
+diff --git a/include/test/helpers/mock_compare.h b/include/test/helpers/mock_compare.h
+index 9be92ee0..80f78a01 100644
+--- a/include/test/helpers/mock_compare.h
++++ b/include/test/helpers/mock_compare.h
+@@ -2,8 +2,11 @@
+ #define LIBTORRENT_TEST_HELPERS_MOCK_COMPARE_H
+ 
+ #include <algorithm>
++#include <map>
+ #include <type_traits>
+ 
++#include <gtest/gtest.h>
++
+ #include "torrent/event.h"
+ #include "torrent/net/socket_address.h"
+ 
+diff --git a/include/torrent/object_raw_bencode.h b/include/torrent/object_raw_bencode.h
+index c90d6b0b..8201d7d1 100644
+--- a/include/torrent/object_raw_bencode.h
++++ b/include/torrent/object_raw_bencode.h
+@@ -8,6 +8,7 @@
+ #include <cstring>
+ #include <string>
+ #include <torrent/common.h>
++#include <torrent/exceptions.h>
+ 
+ namespace torrent {
+ 
+diff --git a/include/torrent/utils/allocators.h b/include/torrent/utils/allocators.h
+index e77549f3..08b85e78 100644
+--- a/include/torrent/utils/allocators.h
++++ b/include/torrent/utils/allocators.h
+@@ -7,6 +7,7 @@
+ #define LIBTORRENT_UTILS_ALLOCATORS_H
+ 
+ #include <cstddef>
++#include <cstdlib>
+ #include <limits>
+ #include <sys/types.h>
+ 
diff --git a/5pkgs/jesec-rtorrent/libtorrent.nix b/5pkgs/jesec-rtorrent/libtorrent.nix
index fc123ef..7f30e8d 100644
--- a/5pkgs/jesec-rtorrent/libtorrent.nix
+++ b/5pkgs/jesec-rtorrent/libtorrent.nix
@@ -1,22 +1,21 @@
-{
-  lib,
-  stdenv,
-  fetchFromGitHub,
-  cmake,
-  gtest,
-  openssl,
-  zlib,
+{ lib
+, stdenv
+, fetchFromGitHub
+, cmake
+, gtest
+, openssl
+, zlib
 }:
 
 stdenv.mkDerivation rec {
   pname = "jesec-libtorrent";
-  version = "0.13.8-r4-unstable-2023-07-04";
+  version = "0.13.8-r4";
 
   src = fetchFromGitHub {
     owner = "jesec";
     repo = "libtorrent";
-    rev = "35d844d4d78a671f8840fe6ae973ebb39a0e8f34";
-    hash = "sha256-H2oUW9iC2pIUSmP9j0U4RfzO1uiIEWVmeZAfF3Ca48k=";
+    rev = "v${version}";
+    hash = "sha256-jC/hgGSi2qy+ToZgdxl1PhASLYbUL0O8trX0th2v5H0=";
   };
 
   nativeBuildInputs = [
@@ -27,6 +26,9 @@ stdenv.mkDerivation rec {
     openssl
     zlib
   ];
+  patches = [
+    ./ed30d713e06dbe14edffd3afdf465d67ad69174f.patch
+  ];
 
   # Disabled because a test is flaky; see https://github.com/jesec/libtorrent/issues/4.
   # doCheck = true;