Repository: bitcoin/bitcoin (language: cpp)

Issue:
`bitcoind` and `bitcoin-qt` crashes while creating PSBT
`bitcoind` and `bitcoin-qt` crashes when creating a PSBT with `walletcreatefundedpsbt`

**Expected behavior**

`bitcoind` and `bitcoin-qt` should not crash and users should not see assertion errors on mainnet. Failing gracefully with some error message would be better. Although I was not able to reproduce this issue on Fedora so ideally this should be fixed.

**Actual behavior**

Error in bitcoind:

```
Assertion failed: selected_effective_value >= target, file wallet/coinselection.cpp, line 391
```

Error in bitcoin-qt:

![image](https://user-images.githubusercontent.com/94559964/192375828-d8e6f628-f52c-4aa8-ae99-19c2fe1a4934.png)


**To reproduce**

1. Create bitcoin.conf 

```
signet=1
txindex=1

signet.rpcport=38332
rpcuser=user
rpcpassword=pass

fallbackfee=0.0004
```

2. Add descriptor in a wallet using bitcoin-qt console:


```
importmulti '[{ "desc": "sh(multi(2,[aa130fed]02de62833c851509cf13c0c63e0dc3ef7b74f7d86b17e9090fab68c09830cb2f2e,[00890d8d]03cf11a2bc1371dad3691bce59e37d85b12aae6b030b533d4e7c5aedeb47a5ec0e))#j8w6g6zg", "timestamp":"now"}]'
```

3. Create PSBT using bitcoin-qt, click on different options in the error message and you will notice it creates PSBT with no change by clicking 'ignore' 3 times else crashes with others:

```
walletcreatefundedpsbt "[{\"txid\":\"e911734bf711009dd6057c88bc598df2d31c18d1cc6bd6c790a704b519d77e68\",\"vout\":0}]" "[{\"tb1q9fxp2p0sm93jdlul8v3svzn7x69zcd9ljsgans\":\"0.0009\"}]"

```

4. Close bitcoin-qt and open powershell or command prompt to create PSBT:

```
bitcoind.exe
````

```
bitcoin-cli.exe -rpcwallet=M1 walletcreatefundedpsbt "[{\"txid\":\"e911734bf711009dd6057c88bc598df2d31c18d1cc6bd6c790a704b519d77e68\",\"vout\":0}]" "[{\"tb1q9fxp2p0sm93jdlul8v3svzn7x69zcd9ljsgans\":\"0.0009\"}]"
```

**System information**

Windows 10

Bitcoin Core Master Branch




Discussion / hints:
Is your reproduction exactly what you did? Was that with a new blank wallet, or a wallet that has other addresses and existing coins? The descriptor and input in your example do not exist on neither mainnet nor testnet, so it's hard to say why these assertions were hit. It should have exited with insufficient funds and an earlier point in coin selection.
I found this assertion error while trying to create PSBT and spend bitcoin from 2of2 multisig. This multisig was created with public keys from M1 and M2 wallet. Descriptor for multisig was imported before creating PSBT. However, this works if you just import any descriptor as watch only and create PSBT.

All wallets used to test this were legacy wallets with private keys. 

`getwalletinfo` for one of the wallets:

```
{
  "walletname": "M1",
  "walletversion": 169900,
  "format": "bdb",
  "balance": 0.00100000,
  "unconfirmed_balance": 0.00000000,
  "immature_balance": 0.00000000,
  "txcount": 4,
  "keypoololdest": 1664208902,
  "keypoolsize": 1000,
  "hdseedid": "a768bb67bcc9983549f7bb6b2457f4d4d63f2288",
  "keypoolsize_hd_internal": 1000,
  "paytxfee": 0.00000000,
  "private_keys_enabled": true,
  "avoid_reuse": false,
  "scanning": false,
  "descriptors": false,
  "external_signer": false
}
```
I tried this on signet and input, descriptor etc. are for signet.

How did you do the windows build? Cross compiled? MSVC? Something else?

Do you observe the same issue with [24.0rc1](https://bitcoincore.org/bin/bitcoin-core-24.0/test.rc1/)?
Cross compiled using the instructions from this doc: https://github.com/bitcoin/bitcoin/blob/master/doc/build-windows.md#building-for-64-bit-windows

Yes I am able to reproduce the same issue using v24.0rc1. Recorded the screen while testing on v24.0rc1:

**bitcoin-qt**

https://user-images.githubusercontent.com/94559964/192400309-4c835089-2ae1-4ff8-b2da-9c0cba283b90.mp4

**bitcoind**

https://user-images.githubusercontent.com/94559964/192400349-36d0d88f-6886-4a02-8b6c-3f0157eebd50.mp4



Could you provide a stacktrace to see which coin selection algorithm led to this assertion failure?
- Re-compile bitcoin with `--enable-debug` then `make clean all -j8`
- Start `bitcoind`
- Get PID of `bitcoind` with `ps aux | grep bitcoind`
- Attach gdb to `bitcoind` with `gdb -p [PID]`
- Run `set logging enabled on` then type `c` and enter
- Run the RPC command that makes bitcoind crash
- Run `backtrace` in GDB then type `c` and enter
- The backtrace will be stored in `./gdb.txt`
I recompiled bitcoin core with `--enable-debug` :
<details>
  <summary>Options</summary>
  
  ```
  external signer = no
  multiprocess    = no
  with experimental syscall sandbox support = no
  with libs       = yes
  with wallet     = yes
    with sqlite   = yes
    with bdb      = yes
  with gui / qt   = yes
    with qr       = yes
  with zmq        = yes
  with test       = yes
  with fuzz binary = yes
  with bench      = yes
  with upnp       = yes
  with natpmp     = yes
  use asm         = yes
  USDT tracing    = no
  sanitizers      =
  debug enabled   = yes
  gprof enabled   = no
  werror          = no
  LTO             = no

  target os       = mingw32
  build os        = linux-gnu

  CC              = x86_64-w64-mingw32-gcc
  CFLAGS          = -pthread -pipe -std=c11 -O2
  CPPFLAGS        =  -DDEBUG -DDEBUG_LOCKORDER -DDEBUG_LOCKCONTENTION -DRPC_DOC_CHECK -DABORT_ON_FAILED_ASSUME   -DHAVE_BUILD_INFO -D_MT -DWIN32 -D_WINDOWS -D_WIN32_WINNT=0x0601 -D_WIN32_IE=0x0501 -DWIN32_LEAN_AND_MEAN -DNOMINMAX -D_FILE_OFFSET_BITS=64 -DPROVIDE_FUZZ_MAIN_FUNCTION -I/home/test/bitcoin/depends/x86_64-w64-mingw32/include/
  CXX             = x86_64-w64-mingw32-g++-posix -std=c++17
  CXXFLAGS        =   -O0 -g3 -ftrapv  -fstack-reuse=none -Wstack-protector -fstack-protector-all -fcf-protection=full      -fno-extended-identifiers -pipe -std=c++17 -O2
  LDFLAGS         =  -lpthread  -Wl,--enable-reloc-section -Wl,--dynamicbase -Wl,--nxcompat -Wl,--high-entropy-va -pie   -Wl,--major-subsystem-version -Wl,6 -Wl,--minor-subsystem-version -Wl,1 -L/home/test/bitcoin/depends/x86_64-w64-mingw32/lib
  AR              = x86_64-w64-mingw32-ar
  ARFLAGS         = cr
  ```
  </details>
  
  Started `bitcoind` and checked PID in task manager
  
  Downloaded [winlibs](https://winlibs.com/) for mingw 64 with gdb
  
  Attached gdb to bitcoind process with `.\gdb.exe --pid=PID` and enabled logging:
  
  ```
Attaching to process 25120
[New Thread 25120.0x1c04]
[New Thread 25120.0x60a4]
[New Thread 25120.0x1628]
[New Thread 25120.0x6114]
[New Thread 25120.0x3ea8]
[New Thread 25120.0x54fc]
[New Thread 25120.0xee8]
[New Thread 25120.0x35f8]
[New Thread 25120.0x50e8]
[New Thread 25120.0x2688]
[New Thread 25120.0x401c]
[New Thread 25120.0x3198]
[New Thread 25120.0x3e50]
[New Thread 25120.0x2b3c]
[New Thread 25120.0x2d88]
[New Thread 25120.0x5e64]
[New Thread 25120.0x3544]
[New Thread 25120.0x4dd4]
[New Thread 25120.0x501c]
[New Thread 25120.0x2184]
[New Thread 25120.0x29ac]
[New Thread 25120.0x4970]
[New Thread 25120.0x14e8]
[New Thread 25120.0x5e54]
Reading symbols from C:\workspace\bitcoin\bin\bitcoind.exe...
(gdb) set logging enabled on
Copying output to gdb.txt.
Copying debug output to gdb.txt.
(gdb)
```

Run this command:

```
bitcoin-cli.exe -rpcwallet=M1 walletcreatefundedpsbt "[{\"txid\":\"e911734bf711009dd6057c88bc598df2d31c18d1cc6bd6c790a704b519d77e68\",\"vout\":0}]" "[{\"tb1q9fxp2p0sm93jdlul8v3svzn7x69zcd9ljsgans\":\"0.0009\"}]"
```

Followed by `backtrace` and `c`

`bitcoind` crashed with error: `Assertion failed: selected_effective_value >= target, file wallet/coinselection.cpp, line 391`

This is gdb.txt:

```
#0  0x00007ffbdee87791 in ntdll!DbgBreakPoint () from C:\WINDOWS\SYSTEM32\ntdll.dll
#1  0x00007ffbdeeb858e in ntdll!DbgUiRemoteBreakin () from C:\WINDOWS\SYSTEM32\ntdll.dll
#2  0x00007ffbddde54e0 in KERNEL32!BaseThreadInitThunk () from C:\WINDOWS\System32\kernel32.dll
#3  0x00007ffbdede485b in ntdll!RtlUserThreadStart () from C:\WINDOWS\SYSTEM32\ntdll.dll
#4  0x0000000000000000 in ?? ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
Continuing.
[New Thread 25120.0x1490]
[Thread 25120.0x5e54 exited with code 0]
[New Thread 25120.0x2fec]
[Thread 25120.0x3e50 exited with code 3]
[Thread 25120.0x6114 exited with code 3]
[Thread 25120.0x4970 exited with code 3]
[Thread 25120.0x4dd4 exited with code 3]
[Thread 25120.0x60a4 exited with code 3]
[Thread 25120.0x3384 exited with code 3]
[Thread 25120.0x1c04 exited with code 3]
[Thread 25120.0xee8 exited with code 3]
[Thread 25120.0x35f8 exited with code 3]
[Thread 25120.0x3198 exited with code 3]
[Thread 25120.0x1628 exited with code 3]
[Thread 25120.0x2688 exited with code 3]
[Thread 25120.0x2d88 exited with code 3]
[Thread 25120.0x5e64 exited with code 3]
[Thread 25120.0x3544 exited with code 3]
[Thread 25120.0x401c exited with code 3]
[Thread 25120.0x29ac exited with code 3]
[Thread 25120.0x2184 exited with code 3]
[Thread 25120.0x14e8 exited with code 3]
[Thread 25120.0x50e8 exited with code 3]
[Thread 25120.0x501c exited with code 3]
[Thread 25120.0x54fc exited with code 3]
[Thread 25120.0x3ea8 exited with code 3]
[Thread 25120.0x1490 exited with code 3]
[Thread 25120.0x2fec exited with code 3]
[Inferior 1 (process 25120) exited with code 03]
```
  
  
  
  

I think you forgot to run `c` after `set logging enabled on` in GDB
```
.\gdb.exe --pid=10784
```

```
Attaching to process 10784
[New Thread 10784.0x5490]
[New Thread 10784.0x810]
[New Thread 10784.0x4054]
[New Thread 10784.0xc08]
[New Thread 10784.0x5330]
[New Thread 10784.0xf24]
[New Thread 10784.0x36b8]
[New Thread 10784.0x3e90]
[New Thread 10784.0x5588]
[New Thread 10784.0x397c]
[New Thread 10784.0x427c]
[New Thread 10784.0x5028]
[New Thread 10784.0x4504]
[New Thread 10784.0x20d4]
[New Thread 10784.0x22d8]
[New Thread 10784.0x1c64]
[New Thread 10784.0x2930]
[New Thread 10784.0x1698]
[New Thread 10784.0x1e88]
[New Thread 10784.0x5db4]
[New Thread 10784.0x4ec4]
[New Thread 10784.0x5b58]
[New Thread 10784.0x7d0]
[New Thread 10784.0x577c]
[New Thread 10784.0x56b8]
[New Thread 10784.0x1cbc]
[New Thread 10784.0xc68]
Reading symbols from C:\workspace\bitcoin\bin\bitcoind.exe...
(gdb) set logging enabled on
Copying output to gdb.txt.
Copying debug output to gdb.txt.
(gdb) c
Continuing.
[Thread 10784.0xc68 exited with code 0]
```
Tried `walletcreatefundedpsbt` in bitcoin-cli after this

```
[Thread 10784.0x810 exited with code 3]
[Thread 10784.0x5490 exited with code 3]
[Thread 10784.0xf4c exited with code 3]
[Thread 10784.0x36b8 exited with code 3]
[Thread 10784.0x4054 exited with code 3]
[Thread 10784.0x5330 exited with code 3]
[Thread 10784.0xf24 exited with code 3]
[Thread 10784.0xc08 exited with code 3]
[Thread 10784.0x427c exited with code 3]
[Thread 10784.0x5028 exited with code 3]
[Thread 10784.0x5588 exited with code 3]
[Thread 10784.0x3e90 exited with code 3]
[Thread 10784.0x2930 exited with code 3]
[Thread 10784.0x4504 exited with code 3]
[Thread 10784.0x22d8 exited with code 3]
[Thread 10784.0x20d4 exited with code 3]
[Thread 10784.0x1e88 exited with code 3]
[Thread 10784.0x397c exited with code 3]
[Thread 10784.0x7d0 exited with code 3]
[Thread 10784.0x5b58 exited with code 3]
[Thread 10784.0x56b8 exited with code 3]
[Thread 10784.0x4ec4 exited with code 3]
[Thread 10784.0x577c exited with code 3]
[Thread 10784.0x5db4 exited with code 3]
[Thread 10784.0x1cbc exited with code 3]
[Thread 10784.0x1698 exited with code 3]
[Inferior 1 (process 10784) exited with code 03]
(gdb)
```

`bitcoind` crashed with same assertion error.
Here's a backtrace with a (debug) build of master (9fcdb9f3a044330d3d7515fa35709102c98534d2):
```bash
Assertion failed: (selected_effective_value >= target), function GetSelectionWaste, file coinselection.cpp, line 391.
Process 47885 stopped
* thread #37, name = 'b-qt-rpcconsole', stop reason = hit program assert
    frame #4: 0x000000010146fc88 bitcoin-qt`wallet::GetSelectionWaste(inputs=size=1, change_cost=0, target=91680, use_effective_value=true) at coinselection.cpp:391:9
   388 	        waste += change_cost;
   389 	    } else {
   390 	        // When we are not making change (change_cost == 0), consider the excess we are throwing away to fees
-> 391 	        assert(selected_effective_value >= target);
   392 	        waste += selected_effective_value - target;
   393 	    }
   394 	
Target 0: (bitcoin-qt) stopped.
(lldb) bt
* thread #37, name = 'b-qt-rpcconsole', stop reason = hit program assert
    frame #0: 0x000000019f4cad98 libsystem_kernel.dylib`__pthread_kill + 8
    frame #1: 0x000000019f4ffee0 libsystem_pthread.dylib`pthread_kill + 288
    frame #2: 0x000000019f43a340 libsystem_c.dylib`abort + 168
    frame #3: 0x000000019f439754 libsystem_c.dylib`__assert_rtn + 272
  * frame #4: 0x000000010146fc88 bitcoin-qt`wallet::GetSelectionWaste(inputs=size=1, change_cost=0, target=91680, use_effective_value=true) at coinselection.cpp:391:9
    frame #5: 0x000000010146d79c bitcoin-qt`wallet::SelectionResult::ComputeAndSetWaste(this=0x00000001712bd738, min_viable_change=980, change_cost=1920, change_fee=1240) at coinselection.cpp:416:19
    frame #6: 0x00000001012aea90 bitcoin-qt`wallet::SelectCoins(wallet=0x000000013f3f9060, available_coins=0x00000001712bdfb0, nTargetValue=0x00000001712bdfd0, coin_control=0x00000001712c0330, coin_selection_params=0x00000001712be418) at spend.cpp:586:16
    frame #7: 0x00000001012b0e40 bitcoin-qt`wallet::CreateTransactionInternal(wallet=0x000000013f3f9060, vecSend=size=1, change_pos=-1, coin_control=0x00000001712c0330, sign=false) at spend.cpp:900:45
    frame #8: 0x00000001012afce8 bitcoin-qt`wallet::CreateTransaction(wallet=0x000000013f3f9060, vecSend=size=1, change_pos=-1, coin_control=0x00000001712c0330, sign=false) at spend.cpp:1055:16
    frame #9: 0x00000001012b241c bitcoin-qt`wallet::FundTransaction(wallet=0x000000013f3f9060, tx=0x00000001712c0cb8, nFeeRet=0x00000001712c0d10, nChangePosInOut=0x00000001712c0d0c, error=0x00000001712bf800, lockUnspents=false, setSubtractFeeFromOutputs=size=0, coinControl=CCoinControl @ 0x00000001712c0330) at spend.cpp:1116:16
    frame #10: 0x00000001011168cc bitcoin-qt`wallet::FundTransaction(wallet=0x000000013f3f9060, tx=0x00000001712c0cb8, fee_out=0x00000001712c0d10, change_position=0x00000001712c0d0c, options=0x00000001712c0d30, coinControl=0x00000001712c0e80, override_min_fee=true) at spend.cpp:701:10
    frame #11: 0x000000010116cbbc bitcoin-qt`wallet::walletcreatefundedpsbt(this=0x00000001712c13a0, self=0x00000001712c1380, request=0x00000001712c16c0)::$_8::operator()(RPCHelpMan const&, JSONRPCRequest const&) const at spend.cpp:1663:5
    frame #12: 0x000000010116c738 bitcoin-qt`decltype(__f=0x00000001712c13a0, __args=0x00000001712c1380, __args=0x00000001712c16c0)::$_8&>(fp)(static_cast<RPCHelpMan const&>(fp0), static_cast<JSONRPCRequest const&>(fp0))) std::__1::__invoke<wallet::walletcreatefundedpsbt()::$_8&, RPCHelpMan const&, JSONRPCRequest const&>(wallet::walletcreatefundedpsbt()::$_8&, RPCHelpMan const&, JSONRPCRequest const&) at type_traits:3918:1
    frame #13: 0x000000010116c670 bitcoin-qt`UniValue std::__1::__invoke_void_return_wrapper<UniValue, false>::__call<wallet::walletcreatefundedpsbt(__args=0x00000001712c13a0, __args=0x00000001712c1380, __args=0x00000001712c16c0)::$_8&, RPCHelpMan const&, JSONRPCRequest const&>(wallet::walletcreatefundedpsbt()::$_8&, RPCHelpMan const&, JSONRPCRequest const&) at invoke.h:30:16
    frame #14: 0x000000010116c5e0 bitcoin-qt`std::__1::__function::__alloc_func<wallet::walletcreatefundedpsbt()::$_8, std::__1::allocator<wallet::walletcreatefundedpsbt()::$_8>, UniValue (RPCHelpMan const&, JSONRPCRequest const&)>::operator(this=0x00000001712c13a0, __arg=0x00000001712c1380, __arg=0x00000001712c16c0)(RPCHelpMan const&, JSONRPCRequest const&) at function.h:178:16
    frame #15: 0x0000000101169c60 bitcoin-qt`std::__1::__function::__func<wallet::walletcreatefundedpsbt()::$_8, std::__1::allocator<wallet::walletcreatefundedpsbt()::$_8>, UniValue (RPCHelpMan const&, JSONRPCRequest const&)>::operator(this=0x00000001712c1398, __arg=0x00000001712c1380, __arg=0x00000001712c16c0)(RPCHelpMan const&, JSONRPCRequest const&) at function.h:352:12
    frame #16: 0x0000000101673754 bitcoin-qt`std::__1::__function::__value_func<UniValue (RPCHelpMan const&, JSONRPCRequest const&)>::operator(this=0x00000001712c1398, __args=0x00000001712c1380, __args=0x00000001712c16c0)(RPCHelpMan const&, JSONRPCRequest const&) const at function.h:505:16
    frame #17: 0x000000010165de3c bitcoin-qt`std::__1::function<UniValue (RPCHelpMan const&, JSONRPCRequest const&)>::operator(this= Lambda in File spend.cpp at Line 1630, __arg=0x00000001712c1380, __arg=0x00000001712c16c0)(RPCHelpMan const&, JSONRPCRequest const&) const at function.h:1182:12
    frame #18: 0x000000010165d120 bitcoin-qt`RPCHelpMan::HandleRequest(this=0x00000001712c1380, request=0x00000001712c16c0) const at util.cpp:585:26
    frame #19: 0x0000000100ae880c bitcoin-qt`CRPCCommand::CRPCCommand(this=0x0000000101f8cc38, request=0x00000001712c16c0, result=0x00000001712c2410, (null)=true)())::'lambda'(JSONRPCRequest const&, UniValue&, bool)::operator()(JSONRPCRequest const&, UniValue&, bool) const at server.h:109:91
    frame #20: 0x0000000100ae8724 bitcoin-qt`decltype(__f=0x0000000101f8cc38, __args=0x00000001712c16c0, __args=0x00000001712c2410, __args=0x00000001712c1647)())::'lambda'(JSONRPCRequest const&, UniValue&, bool)&>(fp)(static_cast<JSONRPCRequest const&>(fp0), static_cast<UniValue&>(fp0), static_cast<bool>(fp0))) std::__1::__invoke<CRPCCommand::CRPCCommand(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, RPCHelpMan (*)())::'lambda'(JSONRPCRequest const&, UniValue&, bool)&, JSONRPCRequest const&, UniValue&, bool>(CRPCCommand::CRPCCommand(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, RPCHelpMan (*)())::'lambda'(JSONRPCRequest const&, UniValue&, bool)&, JSONRPCRequest const&, UniValue&, bool&&) at type_traits:3918:1
    frame #21: 0x0000000100ae8648 bitcoin-qt`bool std::__1::__invoke_void_return_wrapper<bool, false>::__call<CRPCCommand::CRPCCommand(__args=0x0000000101f8cc38, __args=0x00000001712c16c0, __args=0x00000001712c2410, __args=0x00000001712c1647)())::'lambda'(JSONRPCRequest const&, UniValue&, bool)&, JSONRPCRequest const&, UniValue&, bool>(CRPCCommand::CRPCCommand(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, RPCHelpMan (*)())::'lambda'(JSONRPCRequest const&, UniValue&, bool)&, JSONRPCRequest const&, UniValue&, bool&&) at invoke.h:30:16
    frame #22: 0x0000000100ae84f4 bitcoin-qt`std::__1::__function::__alloc_func<CRPCCommand::CRPCCommand(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, RPCHelpMan (*)())::'lambda'(JSONRPCRequest const&, UniValue&, bool), std::__1::allocator<CRPCCommand::CRPCCommand(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, RPCHelpMan (*)())::'lambda'(JSONRPCRequest const&, UniValue&, bool)>, bool (JSONRPCRequest const&, UniValue&, bool)>::operator(this=0x0000000101f8cc38, __arg=0x00000001712c16c0, __arg=0x00000001712c2410, __arg=0x00000001712c1647)(JSONRPCRequest const&, UniValue&, bool&&) at function.h:178:16
    frame #23: 0x0000000100ae5b48 bitcoin-qt`std::__1::__function::__func<CRPCCommand::CRPCCommand(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, RPCHelpMan (*)())::'lambda'(JSONRPCRequest const&, UniValue&, bool), std::__1::allocator<CRPCCommand::CRPCCommand(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, RPCHelpMan (*)())::'lambda'(JSONRPCRequest const&, UniValue&, bool)>, bool (JSONRPCRequest const&, UniValue&, bool)>::operator(this=0x0000000101f8cc30, __arg=0x00000001712c16c0, __arg=0x00000001712c2410, __arg=0x00000001712c1647)(JSONRPCRequest const&, UniValue&, bool&&) at function.h:352:12
    frame #24: 0x0000000100fa519c bitcoin-qt`std::__1::__function::__value_func<bool (JSONRPCRequest const&, UniValue&, bool)>::operator(this=0x0000000101f8cc30, __args=0x00000001712c16c0, __args=0x00000001712c2410, __args=0x00000001712c1647)(JSONRPCRequest const&, UniValue&, bool&&) const at function.h:505:16
    frame #25: 0x0000000100fa2270 bitcoin-qt`std::__1::function<bool (JSONRPCRequest const&, UniValue&, bool)>::operator(this= Function = wallet::walletcreatefundedpsbt() , __arg=0x00000001712c16c0, __arg=0x00000001712c2410, __arg=true)(JSONRPCRequest const&, UniValue&, bool) const at function.h:1182:12
    frame #26: 0x0000000100fa20a4 bitcoin-qt`wallet::(anonymous namespace)::WalletLoaderImpl::registerRpcs(this=0x0000600002df7c48, request=0x00000001712c20a0, result=0x00000001712c2410, last_handler=true)::'lambda'(JSONRPCRequest const&, UniValue&, bool)::operator()(JSONRPCRequest const&, UniValue&, bool) const at interfaces.cpp:541:24
    frame #27: 0x0000000100fa1f80 bitcoin-qt`decltype(__f=0x0000600002df7c48, __args=0x00000001712c20a0, __args=0x00000001712c2410, __args=0x00000001712c1a17)::WalletLoaderImpl::registerRpcs()::'lambda'(JSONRPCRequest const&, UniValue&, bool)&>(fp)(static_cast<JSONRPCRequest const&>(fp0), static_cast<UniValue&>(fp0), static_cast<bool>(fp0))) std::__1::__invoke<wallet::(anonymous namespace)::WalletLoaderImpl::registerRpcs()::'lambda'(JSONRPCRequest const&, UniValue&, bool)&, JSONRPCRequest const&, UniValue&, bool>(wallet::(anonymous namespace)::WalletLoaderImpl::registerRpcs()::'lambda'(JSONRPCRequest const&, UniValue&, bool)&, JSONRPCRequest const&, UniValue&, bool&&) at type_traits:3918:1
    frame #28: 0x0000000100fa1ea4 bitcoin-qt`bool std::__1::__invoke_void_return_wrapper<bool, false>::__call<wallet::(anonymous namespace)::WalletLoaderImpl::registerRpcs(__args=0x0000600002df7c48, __args=0x00000001712c20a0, __args=0x00000001712c2410, __args=0x00000001712c1a17)::'lambda'(JSONRPCRequest const&, UniValue&, bool)&, JSONRPCRequest const&, UniValue&, bool>(wallet::(anonymous namespace)::WalletLoaderImpl::registerRpcs()::'lambda'(JSONRPCRequest const&, UniValue&, bool)&, JSONRPCRequest const&, UniValue&, bool&&) at invoke.h:30:16
    frame #29: 0x0000000100fa1d50 bitcoin-qt`std::__1::__function::__alloc_func<wallet::(anonymous namespace)::WalletLoaderImpl::registerRpcs()::'lambda'(JSONRPCRequest const&, UniValue&, bool), std::__1::allocator<wallet::(anonymous namespace)::WalletLoaderImpl::registerRpcs()::'lambda'(JSONRPCRequest const&, UniValue&, bool)>, bool (JSONRPCRequest const&, UniValue&, bool)>::operator(this=0x0000600002df7c48, __arg=0x00000001712c20a0, __arg=0x00000001712c2410, __arg=0x00000001712c1a17)(JSONRPCRequest const&, UniValue&, bool&&) at function.h:178:16
    frame #30: 0x0000000100f9f378 bitcoin-qt`std::__1::__function::__func<wallet::(anonymous namespace)::WalletLoaderImpl::registerRpcs()::'lambda'(JSONRPCRequest const&, UniValue&, bool), std::__1::allocator<wallet::(anonymous namespace)::WalletLoaderImpl::registerRpcs()::'lambda'(JSONRPCRequest const&, UniValue&, bool)>, bool (JSONRPCRequest const&, UniValue&, bool)>::operator(this=0x0000600002df7c40, __arg=0x00000001712c20a0, __arg=0x00000001712c2410, __arg=0x00000001712c1a17)(JSONRPCRequest const&, UniValue&, bool&&) at function.h:352:12
    frame #31: 0x0000000100fa519c bitcoin-qt`std::__1::__function::__value_func<bool (JSONRPCRequest const&, UniValue&, bool)>::operator(this=0x0000600002df7c40, __args=0x00000001712c20a0, __args=0x00000001712c2410, __args=0x00000001712c1a17)(JSONRPCRequest const&, UniValue&, bool&&) const at function.h:505:16
    frame #32: 0x0000000100fa2270 bitcoin-qt`std::__1::function<bool (JSONRPCRequest const&, UniValue&, bool)>::operator(this= Lambda in File interfaces.cpp at Line 538, __arg=0x00000001712c20a0, __arg=0x00000001712c2410, __arg=true)(JSONRPCRequest const&, UniValue&, bool) const at function.h:1182:12
    frame #33: 0x00000001007c9b70 bitcoin-qt`node::(anonymous namespace)::RpcHandlerImpl::RpcHandlerImpl(this=0x0000600002df7a40, request=0x00000001712c20a0, result=0x00000001712c2410, last_handler=true)::'lambda'(JSONRPCRequest const&, UniValue&, bool)::operator()(JSONRPCRequest const&, UniValue&, bool) const at interfaces.cpp:469:24
    frame #34: 0x00000001007c9a64 bitcoin-qt`decltype(__f=0x0000600002df7a40, __args=0x00000001712c20a0, __args=0x00000001712c2410, __args=0x00000001712c1ce7)::RpcHandlerImpl::RpcHandlerImpl(CRPCCommand const&)::'lambda'(JSONRPCRequest const&, UniValue&, bool)&>(fp)(static_cast<JSONRPCRequest const&>(fp0), static_cast<UniValue&>(fp0), static_cast<bool>(fp0))) std::__1::__invoke<node::(anonymous namespace)::RpcHandlerImpl::RpcHandlerImpl(CRPCCommand const&)::'lambda'(JSONRPCRequest const&, UniValue&, bool)&, JSONRPCRequest const&, UniValue&, bool>(node::(anonymous namespace)::RpcHandlerImpl::RpcHandlerImpl(CRPCCommand const&)::'lambda'(JSONRPCRequest const&, UniValue&, bool)&, JSONRPCRequest const&, UniValue&, bool&&) at type_traits:3918:1
    frame #35: 0x00000001007c9988 bitcoin-qt`bool std::__1::__invoke_void_return_wrapper<bool, false>::__call<node::(anonymous namespace)::RpcHandlerImpl::RpcHandlerImpl(__args=0x0000600002df7a40, __args=0x00000001712c20a0, __args=0x00000001712c2410, __args=0x00000001712c1ce7)::'lambda'(JSONRPCRequest const&, UniValue&, bool)&, JSONRPCRequest const&, UniValue&, bool>(node::(anonymous namespace)::RpcHandlerImpl::RpcHandlerImpl(CRPCCommand const&)::'lambda'(JSONRPCRequest const&, UniValue&, bool)&, JSONRPCRequest const&, UniValue&, bool&&) at invoke.h:30:16
    frame #36: 0x00000001007c988c bitcoin-qt`std::__1::__function::__alloc_func<node::(anonymous namespace)::RpcHandlerImpl::RpcHandlerImpl(CRPCCommand const&)::'lambda'(JSONRPCRequest const&, UniValue&, bool), std::__1::allocator<node::(anonymous namespace)::RpcHandlerImpl::RpcHandlerImpl(CRPCCommand const&)::'lambda'(JSONRPCRequest const&, UniValue&, bool)>, bool (JSONRPCRequest const&, UniValue&, bool)>::operator(this=0x0000600002df7a40, __arg=0x00000001712c20a0, __arg=0x00000001712c2410, __arg=0x00000001712c1ce7)(JSONRPCRequest const&, UniValue&, bool&&) at function.h:178:16
    frame #37: 0x00000001007c6ee0 bitcoin-qt`std::__1::__function::__func<node::(anonymous namespace)::RpcHandlerImpl::RpcHandlerImpl(CRPCCommand const&)::'lambda'(JSONRPCRequest const&, UniValue&, bool), std::__1::allocator<node::(anonymous namespace)::RpcHandlerImpl::RpcHandlerImpl(CRPCCommand const&)::'lambda'(JSONRPCRequest const&, UniValue&, bool)>, bool (JSONRPCRequest const&, UniValue&, bool)>::operator(this=0x0000600002df7a38, __arg=0x00000001712c20a0, __arg=0x00000001712c2410, __arg=0x00000001712c1ce7)(JSONRPCRequest const&, UniValue&, bool&&) at function.h:352:12
    frame #38: 0x0000000100fa519c bitcoin-qt`std::__1::__function::__value_func<bool (JSONRPCRequest const&, UniValue&, bool)>::operator(this=0x0000600002df7a38, __args=0x00000001712c20a0, __args=0x00000001712c2410, __args=0x00000001712c1ce7)(JSONRPCRequest const&, UniValue&, bool&&) const at function.h:505:16
    frame #39: 0x0000000100fa2270 bitcoin-qt`std::__1::function<bool (JSONRPCRequest const&, UniValue&, bool)>::operator(this= Lambda in File interfaces.cpp at Line 466, __arg=0x00000001712c20a0, __arg=0x00000001712c2410, __arg=true)(JSONRPCRequest const&, UniValue&, bool) const at function.h:1182:12
    frame #40: 0x0000000100af7ff4 bitcoin-qt`ExecuteCommand(command=0x0000600002df7a08, request=0x00000001712c20a0, result=0x00000001712c2410, last_handler=true) at server.cpp:475:20
    frame #41: 0x0000000100ad5a80 bitcoin-qt`ExecuteCommands(commands=size=1, request=0x00000001712c20a0, result=0x00000001712c2410) at server.cpp:440:13
    frame #42: 0x0000000100ad57d4 bitcoin-qt`CRPCTable::execute(this=0x0000000101f906d8, request=0x00000001712c20a0) const at server.cpp:460:13
    frame #43: 0x000000010078c374 bitcoin-qt`node::(anonymous namespace)::NodeImpl::executeRpc(this=0x000060000003b960, command="walletcreatefundedpsbt", params=0x00000001712c2500, uri="/wallet/BDB") at interfaces.cpp:316:27
    frame #44: 0x00000001000f96f0 bitcoin-qt`RPCConsole::RPCParseCommandLine(node=0x000060000003b960, strResult="", strCommand="walletcreatefundedpsbt \"[{\\\"txid\\\":\\\"e911734bf711009dd6057c88bc598df2d31c18d1cc6bd6c790a704b519d77e68\\\",\\\"vout\\\":0}]\" \"[{\\\"tb1q9fxp2p0sm93jdlul8v3svzn7x69zcd9ljsgans\\\":\\\"0.0009\\\"}]\"\n", fExecute=true, pstrFilteredOut="", wallet_model=0x0000600003d20f00) at rpcconsole.cpp:335:48
    frame #45: 0x00000001000fb1c8 bitcoin-qt`RPCConsole::RPCExecuteCommandLine(node=0x000060000003b960, strResult="", strCommand="walletcreatefundedpsbt \"[{\\\"txid\\\":\\\"e911734bf711009dd6057c88bc598df2d31c18d1cc6bd6c790a704b519d77e68\\\",\\\"vout\\\":0}]\" \"[{\\\"tb1q9fxp2p0sm93jdlul8v3svzn7x69zcd9ljsgans\\\":\\\"0.0009\\\"}]\"\n", pstrFilteredOut="", wallet_model=0x0000600003d20f00) at rpcconsole.h:53:16
    frame #46: 0x00000001000f7f6c bitcoin-qt`RPCExecutor::request(this=0x00006000004d2200, command=0x0000600010f71908, wallet_model=0x0000600003d20f00) at rpcconsole.cpp:441:14
    frame #47: 0x0000000100133374 bitcoin-qt`RPCConsole::on_lineEdit_returnPressed(this=0x0000600010f71900)::$_15::operator()() const at rpcconsole.cpp:1047:21
    frame #48: 0x0000000100133300 bitcoin-qt`QtPrivate::FunctorCall<QtPrivate::IndexesList<>, QtPrivate::List<>, void, RPCConsole::on_lineEdit_returnPressed()::$_15>::call(f=0x0000600010f71900, arg=0x00006000be997ed8)::$_15&, void**) at qobjectdefs_impl.h:146:13
    frame #49: 0x0000000100133240 bitcoin-qt`void QtPrivate::Functor<RPCConsole::on_lineEdit_returnPressed()::$_15, 0>::call<QtPrivate::List<>, void>(f=0x0000600010f71900, (null)=0x00006000004d2200, arg=0x00006000be997ed8)::$_15&, void*, void**) at qobjectdefs_impl.h:256:13
    frame #50: 0x000000010013317c bitcoin-qt`QtPrivate::QFunctorSlotObject<RPCConsole::on_lineEdit_returnPressed()::$_15, 0, QtPrivate::List<>, void>::impl(which=1, this_=0x0000600010f718f0, r=0x00006000004d2200, a=0x00006000be997ed8, ret=0x0000000000000000) at qobjectdefs_impl.h:443:17
    frame #51: 0x0000000106eea72c QtCore`QObject::event(QEvent*) + 596
    frame #52: 0x0000000107287560 QtWidgets`QApplicationPrivate::notify_helper(QObject*, QEvent*) + 292
    frame #53: 0x0000000107288904 QtWidgets`QApplication::notify(QObject*, QEvent*) + 548
    frame #54: 0x0000000106ec06c8 QtCore`QCoreApplication::notifyInternal2(QObject*, QEvent*) + 292
    frame #55: 0x0000000106ec1b90 QtCore`QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) + 1404
    frame #56: 0x0000000106f21300 QtCore`QEventDispatcherUNIX::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) + 84
    frame #57: 0x0000000106ebc684 QtCore`QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) + 520
    frame #58: 0x0000000106cff1dc QtCore`QThread::exec() + 136
    frame #59: 0x0000000106d001d8 QtCore`___lldb_unnamed_symbol7202 + 348
    frame #60: 0x000000019f50026c libsystem_pthread.dylib`_pthread_start + 148
```
Yes I'm not sure you were using a debug build @1440000bytes, did you `make clean` after reconfiguring with `--enable-debug`?
It's fine anyway now.
I missed that step. 

Fix patch:
diff --git a/src/wallet/spend.cpp b/src/wallet/spend.cpp
index 523db8d7bc782..6833f9a095503 100644
--- a/src/wallet/spend.cpp
+++ b/src/wallet/spend.cpp
@@ -582,7 +582,13 @@ std::optional<SelectionResult> SelectCoins(const CWallet& wallet, CoinsResult& a
     if (coin_control.HasSelected() && !coin_control.m_allow_other_inputs) {
         SelectionResult result(nTargetValue, SelectionAlgorithm::MANUAL);
         result.AddInput(preset_inputs);
-        if (result.GetSelectedValue() < nTargetValue) return std::nullopt;
+
+        if (!coin_selection_params.m_subtract_fee_outputs && result.GetSelectedEffectiveValue() < nTargetValue) {
+            return std::nullopt;
+        } else if (result.GetSelectedValue() < nTargetValue) {
+            return std::nullopt;
+        }
+
         result.ComputeAndSetWaste(coin_selection_params.min_viable_change, coin_selection_params.m_cost_of_change, coin_selection_params.m_change_fee);
         return result;
     }


Test patch:
diff --git a/src/wallet/test/coinselector_tests.cpp b/src/wallet/test/coinselector_tests.cpp
index 30a7fb9eb6ec8..23f024247d72e 100644
--- a/src/wallet/test/coinselector_tests.cpp
+++ b/src/wallet/test/coinselector_tests.cpp
@@ -922,5 +922,52 @@ BOOST_AUTO_TEST_CASE(effective_value_test)
     BOOST_CHECK_EQUAL(output5.GetEffectiveValue(), nValue); // The effective value should be equal to the absolute value if input_bytes is -1
 }
 
+BOOST_AUTO_TEST_CASE(SelectCoins_effective_value_test)
+{
+    // Test that the effective value is used to check whether preset inputs provide sufficient funds when subtract_fee_outputs is not used.
+    // This test creates a coin whose value is higher than the target but whose effective value is lower than the target.
+    // The coin is selected using coin control, with m_allow_other_inputs = false. SelectCoins should fail due to insufficient funds.
+
+    std::unique_ptr<CWallet> wallet = std::make_unique<CWallet>(m_node.chain.get(), "", m_args, CreateMockWalletDatabase());
+    wallet->LoadWallet();
+    LOCK(wallet->cs_wallet);
+    wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
+    wallet->SetupDescriptorScriptPubKeyMans();
+
+    CoinsResult available_coins;
+    {
+        std::unique_ptr<CWallet> dummyWallet = std::make_unique<CWallet>(m_node.chain.get(), "dummy", m_args, CreateMockWalletDatabase());
+        dummyWallet->LoadWallet();
+        LOCK(dummyWallet->cs_wallet);
+        dummyWallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
+        dummyWallet->SetupDescriptorScriptPubKeyMans();
+
+        add_coin(available_coins, *dummyWallet, 100000); // 0.001 BTC
+    }
+
+    CAmount target{99900}; // 0.000999 BTC
+
+    FastRandomContext rand;
+    CoinSelectionParams cs_params{
+        rand,
+        /*change_output_size=*/34,
+        /*change_spend_size=*/148,
+        /*min_change_target=*/1000,
+        /*effective_feerate=*/CFeeRate(3000),
+        /*long_term_feerate=*/CFeeRate(1000),
+        /*discard_feerate=*/CFeeRate(1000),
+        /*tx_noinputs_size=*/0,
+        /*avoid_partial=*/false,
+    };
+    CCoinControl cc;
+    cc.m_allow_other_inputs = false;
+    COutput output = available_coins.All().at(0);
+    cc.SetInputWeight(output.outpoint, 148);
+    cc.SelectExternal(output.outpoint, output.txout);
+
+    const auto result = SelectCoins(*wallet, available_coins, target, cc, cs_params);
+    BOOST_CHECK(!result);
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 } // namespace wallet