Repository: go
Issue: log/slog: LevelHandler example is invalid; results in a deadlock
Labels: NeedsInvestigation

### What version of Go are you using (`go version`)?

<pre>
$ go version
go version go1.21.0 linux/amd64
</pre>

### Does this issue reproduce with the latest release?
Yes.

### What operating system and processor architecture are you using (`go env`)?

<details><summary><code>go env</code> Output</summary><br><pre>
$ go env
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/ainar/.cache/go-build'
GOENV='/home/ainar/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/ainar/go/pkg/mod'
GONOPROXY='REMOVED'
GONOSUMDB='REMOVED'
GOOS='linux'
GOPATH='/home/ainar/go'
GOPRIVATE='REMOVED'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/ainar/go/go1.21'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/ainar/go/go1.21/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21.0'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/home/ainar/adg/AdGuardDNS/go.mod'
GOWORK='/home/ainar/adg/AdGuardDNS/go.work'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build844166527=/tmp/go-build -gno-record-gcc-switches'
</pre></details>

### What did you do?

https://go.dev/play/p/_usbsfWCk4k

### What did you expect to see?

1.  The example from [here](https://cs.opensource.google/go/x/exp/+/master:slog/example_level_handler_test.go;drc=dd950f8aeaeae96f4a4c0b979152048f8f364fa8) to compile with the final `log/slog`.

2.  That example to work with the final `log/slog` without [known deadlocks](https://github.com/golang/go/issues/56345#issuecomment-1357709117).

### What did you see instead?

1.  The example had to be modified to include `context.Context`s, see the code in the playground.

2.  The code still results in:

    ```none
    fatal error: all goroutines are asleep - deadlock!

    goroutine 1 [sync.Mutex.Lock]:
    sync.runtime_SemacquireMutex(0x11?, 0x20?, 0x10?)
    	/usr/local/go-faketime/src/runtime/sema.go:77 +0x25
    sync.(*Mutex).lockSlow(0xc0000160f0)
    	/usr/local/go-faketime/src/sync/mutex.go:171 +0x15d
    sync.(*Mutex).Lock(...)
    	/usr/local/go-faketime/src/sync/mutex.go:90
    log.(*Logger).output(0xc0000160f0, 0x0, 0x0?, 0xc000050f38)
    	/usr/local/go-faketime/src/log/log.go:243 +0x427
    log.init.0.func1(0xbab699fc00000000?, {0xc00007e800?, 0x583520?, 0xc000012070?})
    	/usr/local/go-faketime/src/log/log.go:251 +0x53
    log/slog.(*defaultHandler).Handle(_, {_, _}, {{0xbab699fc00000000, 0x1, 0x583520}, {0xc000012070, 0xb}, 0x0, 0x0, ...})
    	/usr/local/go-faketime/src/log/slog/handler.go:116 +0x3b2
    main.(*LevelHandler).Handle(_, {_, _}, {{0xbab699fc00000000, 0x1, 0x583520}, {0xc000012070, 0xb}, 0x0, 0x0, ...})
    	/tmp/sandbox2273227079/levelhandler.go:39 +0x55
    log/slog.(*handlerWriter).Write(0xc000060040, {0xc000012060, 0xc, 0x7fa4a0fcb5f0?})
    	/usr/local/go-faketime/src/log/slog/logger.go:70 +0x226
    log.(*Logger).output(0xc0000160f0, 0x4ae051, 0x0?, 0xc000051750)
    	/usr/local/go-faketime/src/log/log.go:245 +0x485
    log.init.0.func1(0xbab699fc00000000?, {0xc00007e000?, 0x583520?, 0x4d0552?})
    	/usr/local/go-faketime/src/log/log.go:251 +0x53
    log/slog.(*defaultHandler).Handle(_, {_, _}, {{0xbab699fc00000000, 0x1, 0x583520}, {0x4d0552, 0x5}, 0xfffffffffffffffc, 0x4ae051, ...})
    	/usr/local/go-faketime/src/log/slog/handler.go:116 +0x3b2
    main.(*LevelHandler).Handle(_, {_, _}, {{0xbab699fc00000000, 0x1, 0x583520}, {0x4d0552, 0x5}, 0xfffffffffffffffc, 0x4ae051, ...})
    	/tmp/sandbox2273227079/levelhandler.go:39 +0x55
    log/slog.(*Logger).log(0xc0000140a0, {0x4f51f0, 0x5b0e40}, 0xfffffffffffffffc, {0x4d0552, 0x5}, {0x0, 0x0, 0x0})
    	/usr/local/go-faketime/src/log/slog/logger.go:225 +0x1e4
    log/slog.Debug({0x4d0552?, 0x0?}, {0x0?, 0x0?, 0x60?})
    	/usr/local/go-faketime/src/log/slog/logger.go:250 +0x96
    main.main()
    	/tmp/sandbox2273227079/prog.go:10 +0x131
    ```

    In the original issue, @jba [had said](https://github.com/golang/go/issues/56345#issuecomment-1357756253) that it should be possible to fix this deadlock once `slog` is imported into stdlib, but it seems like a test case for this example hadn't been made?

### Additional information

Originally, my need for this code is from the [desire to use the default plain-text handler of `log/slog` with debug level](https://github.com/golang/go/issues/56345#issuecomment-1302023793).